@Test public void findLongestCommonSubstrings() { assertTrue(Utils.findLongestCommonSubstrings("abab", "baba").contains("aba")); assertTrue(Utils.findLongestCommonSubstrings("abab", "baba").contains("bab")); assertEquals(2, Utils.findLongestCommonSubstrings("abab", "baba").size()); assertTrue(Utils.findLongestCommonSubstrings("abcde", "bcd").contains("bcd")); assertTrue(Utils.findLongestCommonSubstrings("bcd", "abcde").contains("bcd")); assertEquals(1, Utils.findLongestCommonSubstrings("one", "chosenone").size()); assertEquals(0, Utils.findLongestCommonSubstrings("abc", "xyz").size()); }
public static Set<String> findLongestCommonSubstrings(String s, String t) { int[][] table = new int[s.length()][t.length()]; int longest = 0; Set<String> result = new HashSet<>(); for (int i = 0; i < s.length(); i++) { for (int j = 0; j < t.length(); j++) { if (s.charAt(i) != t.charAt(j)) { continue; } table[i][j] = (i == 0 || j == 0) ? 1 : 1 + table[i - 1][j - 1]; if (table[i][j] > longest) { longest = table[i][j]; result.clear(); } if (table[i][j] == longest) { result.add(s.substring(i - longest + 1, i + 1)); } } } return result; }
Utils { public static Set<String> findLongestCommonSubstrings(String s, String t) { int[][] table = new int[s.length()][t.length()]; int longest = 0; Set<String> result = new HashSet<>(); for (int i = 0; i < s.length(); i++) { for (int j = 0; j < t.length(); j++) { if (s.charAt(i) != t.charAt(j)) { continue; } table[i][j] = (i == 0 || j == 0) ? 1 : 1 + table[i - 1][j - 1]; if (table[i][j] > longest) { longest = table[i][j]; result.clear(); } if (table[i][j] == longest) { result.add(s.substring(i - longest + 1, i + 1)); } } } return result; } }
Utils { public static Set<String> findLongestCommonSubstrings(String s, String t) { int[][] table = new int[s.length()][t.length()]; int longest = 0; Set<String> result = new HashSet<>(); for (int i = 0; i < s.length(); i++) { for (int j = 0; j < t.length(); j++) { if (s.charAt(i) != t.charAt(j)) { continue; } table[i][j] = (i == 0 || j == 0) ? 1 : 1 + table[i - 1][j - 1]; if (table[i][j] > longest) { longest = table[i][j]; result.clear(); } if (table[i][j] == longest) { result.add(s.substring(i - longest + 1, i + 1)); } } } return result; }  }
Utils { public static Set<String> findLongestCommonSubstrings(String s, String t) { int[][] table = new int[s.length()][t.length()]; int longest = 0; Set<String> result = new HashSet<>(); for (int i = 0; i < s.length(); i++) { for (int j = 0; j < t.length(); j++) { if (s.charAt(i) != t.charAt(j)) { continue; } table[i][j] = (i == 0 || j == 0) ? 1 : 1 + table[i - 1][j - 1]; if (table[i][j] > longest) { longest = table[i][j]; result.clear(); } if (table[i][j] == longest) { result.add(s.substring(i - longest + 1, i + 1)); } } } return result; }  @Deprecated static boolean instanceOf(String type, Object object); static File buildAbsoluteFile(File parentDir, String filePath); static String computeHash(File file); static Set<String> findLongestCommonSubstrings(String s, String t); static int getLongestCommonSubstringLength(String s, String t); static Identifier extractIdentifierFromDcString(String string); static String mergeStrings(List<String> list, Character separator); static String listToString(List list); static void deleteNonemptyDir(File unzippedDir); static void unzip(File zipFile, File outFolder); }
Utils { public static Set<String> findLongestCommonSubstrings(String s, String t) { int[][] table = new int[s.length()][t.length()]; int longest = 0; Set<String> result = new HashSet<>(); for (int i = 0; i < s.length(); i++) { for (int j = 0; j < t.length(); j++) { if (s.charAt(i) != t.charAt(j)) { continue; } table[i][j] = (i == 0 || j == 0) ? 1 : 1 + table[i - 1][j - 1]; if (table[i][j] > longest) { longest = table[i][j]; result.clear(); } if (table[i][j] == longest) { result.add(s.substring(i - longest + 1, i + 1)); } } } return result; }  @Deprecated static boolean instanceOf(String type, Object object); static File buildAbsoluteFile(File parentDir, String filePath); static String computeHash(File file); static Set<String> findLongestCommonSubstrings(String s, String t); static int getLongestCommonSubstringLength(String s, String t); static Identifier extractIdentifierFromDcString(String string); static String mergeStrings(List<String> list, Character separator); static String listToString(List list); static void deleteNonemptyDir(File unzippedDir); static void unzip(File zipFile, File outFolder);  }