public class Solution {
public static void main(String[] args) {
String str1="ABCBDAB";
String str2="BDCABA";
System.out.println(longestCommonSubsequence(str1, str2));
}
public static int longestCommonSubsequence(String str1, String str2) {
int[][] LCS=new int[str1.length()+1][str2.length()+1];
//System.out.println(str1);
if(str1==null||str2==null)
return 0;
for(int i=0;i<=str1.length();i++)
{
LCS[i][0]=0;
}
for(int j=0;j<=str2.length();j++)
{
LCS[0][j]=0;
}
for(int i=1;i<=str1.length();i++)
{
for(int j=1;j<=str2.length();j++)
{
if(str1.charAt(i-1)==str2.charAt(j-1))
{
LCS[i][j]=LCS[i-1][j-1]+1;
}
else
LCS[i][j]=Math.max(LCS[i][j-1], LCS[i-1][j]);
}
}
return LCS[str1.length()][str2.length()];
}
}