两字符串寻找最大相同子串

1.s1.contains(sub)  存在返回true

2.把较短字符串先与长的contains,存在则输出;不存在则减一再contains


public class StringTest_3 {

public static void main(String[] args) {
String s1="jielLeaHappyChildDayChildDayewqdx";
String s2="IslsamdfLeaHappyChildDayge";

String s=getMaxSubstring(s1,s2);
System.out.println("相同最大串为:" +s);
}

public static String getMaxSubstring(String s1, String s2) {

                                                                                  // 控制初始和结尾,同时向右平移,且不让结尾超出长字符串 

for (int i = 0; i < s2.length(); i++) {                                                    //控制要减得长度

for (int a = 0, b = s2.length() - i; b != s2.length() + 1; a++, b++)   

{
String sub = s2.substring(a, b);
// System.out.println(sub);
if (s1.contains(sub))
return sub;
}
}
return null;
}

}

运行结果:

两字符串寻找最大相同子串_第1张图片

题目出处:毕向东-15-11

你可能感兴趣的:(两字符串寻找最大相同子串)