Skip to content

Commit 9b77675

Browse files
authored
Merge pull request #13 from jotdeep/main
Added Longest Common Subsequence
2 parents f23fe73 + 438e9dc commit 9b77675

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed

LongestCommonSubsequence.java

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import java.util.*;
2+
3+
class Solution8 {
4+
public int solve(String a, String b) {
5+
int dp[][]=new int[a.length()][b.length()];
6+
for (int i=0;i<=a.length();i++)
7+
{
8+
for (int j=0;j<=b.length();j++)
9+
{
10+
11+
if (i==0||j==0)
12+
{
13+
dp[i][j]=0;
14+
}
15+
else
16+
{
17+
if (a.charAt(i)==b.charAt(j)) {
18+
dp[i][j] = dp[i - 1][j - 1] + 1;
19+
}
20+
else
21+
{
22+
dp[i][j]=Math.max(dp[i-1][j],dp[i][j-1]);
23+
}
24+
}
25+
}
26+
}
27+
return dp[a.length()][b.length()];
28+
}
29+
}

0 commit comments

Comments
 (0)