diff --git a/Java/dynamic-programming/LongestCommonSubsequence.java b/Java/dynamic-programming/LongestCommonSubsequence.java new file mode 100644 index 0000000..d05e317 --- /dev/null +++ b/Java/dynamic-programming/LongestCommonSubsequence.java @@ -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]; + } +}