forked from admirerr/DSA-Collection
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLongestCommonSubsequence.java
More file actions
26 lines (24 loc) · 984 Bytes
/
Copy pathLongestCommonSubsequence.java
File metadata and controls
26 lines (24 loc) · 984 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
public class LongestCommonSubsequence {
public static void main(String[] args) {
String str1 = "AGGTAB";
String str2 = "GXTXAYB";
System.out.println("Length of Longest Common Subsequence between \"" + str1 + "\" and \"" + str2 + "\" is: " + lcs(str1, str2));
}
public static int lcs(String str1, String str2) {
int m = str1.length();
int n = str2.length();
int[][] dp = new int[m + 1][n + 1];
for (int i = 0; i <= m; i++) {
for (int j = 0; j <= n; j++) {
if (i == 0 || j == 0) {
dp[i][j] = 0; // Base case: one string is empty
} else if (str1.charAt(i - 1) == str2.charAt(j - 1)) {
dp[i][j] = dp[i - 1][j - 1] + 1; // Characters match
} else {
dp[i][j] = Math.max(dp[i - 1][j], dp[i][j - 1]); // Characters don't match
}
}
}
return dp[m][n];
}
}