Skip to content

Commit d7963ff

Browse files
authored
Create Longest Common Subsequence
1 parent 1f174b3 commit d7963ff

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed

Longest Common Subsequence

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
class Solution {
2+
public int longestCommonSubsequence(String text1, String text2) {
3+
int i = 0;
4+
int j = 0;
5+
int[][] dp = new int[text1.length()+1][text2.length()+1];
6+
for(i = 1; i<dp.length; i++){
7+
for(j = 1; j<dp[i].length; j++){
8+
//System.out.println(i +" "+ dp.length);
9+
if(text1.charAt(i-1) == text2.charAt(j-1)){
10+
dp[i][j] = dp[i-1][j-1] + 1;
11+
}
12+
else{
13+
dp[i][j] = Math.max(dp[i-1][j], dp[i][j-1]);
14+
}
15+
}
16+
}
17+
return dp[dp.length-1][dp[0].length-1];
18+
}
19+
}

0 commit comments

Comments
 (0)