class Solution { /** Recursion */ public int longestCommonSubsequence(String text1, String text2) { int n = text1.length(); int m = text2.length(); return findLCS(text1, text2, n, m); } public int findLCS(String text1, String text2, int n, int m) { if(n == 0 || m == 0) return 0; // choice diagram // if both character matches, reduce size of both strings if(text1.charAt(n-1) == text2.charAt(m-1)) return 1 + findLCS(text1, text2, n-1, m-1); // if both characters does not match, then reduce lenght of either of string else { int choice1 = findLCS(text1, text2, n-1, m); int choice2 = findLCS(text1, text2, n, m-1); return Math.max(choice1, choice2); } } /** Memoization */ public int longestCommonSubsequence(String text1, String text2) { int n = text1.length(); int m = text2.length(); int[][] t = new int[n+1][m+1]; for(int i=0; i