Created
August 6, 2019 19:09
-
-
Save RitamChakraborty/816b0f5d0a6f3ecb88204427a6d81573 to your computer and use it in GitHub Desktop.
Longest Common Subsequence problem using recursion without using memorization.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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