Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions Java/dynamic-programming/LongestCommonSubsequence.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,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];
}
}