Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save RitamChakraborty/816b0f5d0a6f3ecb88204427a6d81573 to your computer and use it in GitHub Desktop.
Save RitamChakraborty/816b0f5d0a6f3ecb88204427a6d81573 to your computer and use it in GitHub Desktop.
Longest Common Subsequence problem using recursion without using memorization.
public class LongestCommonSubsequenceWithoutMemorization {
private static char[] A;
private static char[] B;
private static int longestCommonSubsequent(int i, int j) {
if (i == A.length || j == B.length) {
return 0;
} else if (A[i] == B[j]) {
return 1 + longestCommonSubsequent(i + 1, j + 1);
} else {
return Integer.max(longestCommonSubsequent(i + 1, j), longestCommonSubsequent(i, j + 1));
}
}
public static void main(String[] args) {
A = "bd".toCharArray();
B = "abcd".toCharArray();
System.out.println(longestCommonSubsequent(0, 0));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment