这才是终极版的获取两个字符串中的最大相同子串,其它的都有漏洞
直接上代码:可以获取多个最大字符串,保证程序的健壮性。
public String[] test3(String a, String b) {
boolean flag = false;
StringBuffer stringBuffer = new StringBuffer();
String max = (a.length() >= b.length()) ? a : b;
String min = (a.length() <= b.length()) ? a : b;
for (int i = 0; i < min.length(); i++) {
for (int j = 0; j < i + 1; j++) {
String x = min.substring(j, j + min.length() - i);
if (max.contains(x)) {
if (!stringBuffer.toString().contains(x))
stringBuffer.append(x + ",");
flag = true;
}
}
if (flag) break;
}
if (flag) {
return stringBuffer.toString().replaceAll("^,|,$", "").split(",");
}
return new String[]{"null"};
}