1
2
3
4
5
6
7
8
9
10
N = str1.length();
M = str2.length();
dp = new int[N+1][M+1];
for(int i=1; i<= N; i++) {
for(int j=1; j<= M; j++) {
if(str1.charAt(i-1) == str2.charAt(j-1)) dp[i][j] = dp[i-1][j-1] + 1;
else dp[i][j] = Math.max(dp[i-1][j], dp[i][j-1]);
}
}