import static java.lang.System.*;
/**
* @Date:CreatedIn 2020/6/29 19:56
* @Description:获取两个字符串中最大相同字串。
*/
public class Test {
public static void main(String[] args) {
String str1 = "abcwerthelloyuiodef";
String str2 = "cvhellobnm";
Test t = new Test();
String str = t.sameStr(str1,str2);
out.println(str);
}
public String sameStr(String str1,String str2) {
if (str1.isEmpty() || str2.isEmpty()) {
return null;
}
String maxStr = str1.length() >= str2.length() ? str1 : str2;
String minStr = str1.length() < str2.length() ? str1 : str2;
int length = minStr.length();
for (int i = 0; i < length; i++) {
for (int x = 0, y = length - i; y <= length; x++, y++) {
String subStr = minStr.substring(x, y);
if (maxStr.contains(subStr)) {
return subStr;
}
}
}
return null;
}
}
java获取两个字符串中最大相同字串
最新推荐文章于 2021-08-13 11:15:17 发布