LongestCommonSubsequence { public static int longestCommonSequence(String str1, String str2) { if (str1 == null || str2 == null || str1.length() == 0 || str2.length() == 0) { return 0; } char[] cs1 = str1.toCharArray(), cs2 = str2.toCharArray(); int[][] longest = new int[str1.length() + 1][str2.length() + 1]; for (int i = 0; i <= str1.length(); i++) { longest[i][0] = 0; } for (int i = 0; i <= str2.length(); i++) { longest[0][i] = 0; } for (int i = 1; i <= str1.length(); i++) { for (int j = 1; j <= str2.length(); j++) { int max = Integer.max(longest[i - 1][j], longest[i][j - 1]); if (cs1[i - 1] == cs2[j - 1]) { max = Integer.max(longest[i - 1][ j - 1] + 1, max); } longest[i][j] = max; } } return longest[str1.length()][str2.length()]; }  static int longestCommonSequence(String str1, String str2);  }
@Test(dataProvider = "longestStrings") public void test(String string1, String string2, int expectedLongestCommonSubsequenceLength) { assertEquals(LongestCommonSubsequence.longestCommonSequence(string1, string2), expectedLongestCommonSubsequenceLength ); }