Python code for finding the longest common subsequence Post.Byes (original) (raw)
I am looking for a Python code to find the longest common subsequence of two strings. I found this blog post that describes the length of longest common subsequence problem and provides a solution in Java. Can anyone help me translate the Java code into Python?
Here is the Java code from the blog post:
Code:
java public static int longestCommonSubsequence(String X, String Y) { int m = X.length(); int n = Y.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; } else if (X.charAt(i - 1) == Y.charAt(j - 1)) { dp[i][j] = dp[i - 1][j - 1] + 1; } else { dp[i][j] = Math.max(dp[i - 1][j], dp[i][j - 1]); } } } return dp[m][n]; }
I would appreciate any help with this. Thank you!