java:寻找两个字符串的最长公共子串
// 找一个字符串的所有子串 public static List<String> findAllStr(String s) { List<String> list = new ArrayList<>(); for (int i = 0; i < s.length(); i++) { for (int j = i + 1; j <= s.length(); j++) { list.add(s.substring(i, j)); } } return list; } // 找两个字符串的公共最长子字符串,寻找最短公共子字符串,修改比较器即可 public static String findComStr(String sOne, String sTwo) { List<String> ls = findAllStr(sOne); Collections.sort(ls, new Comparator<String>() { @Override public int compare(String o1, String o2) { return o2.length() - o1.length(); } }); for (String i : ls) { if (sTwo.contains(i)) { return i; } } return ""; }