//获取两个字符中相同的最大子串
//思路:将短的字符串依次减少与长的字符串进行查找indexOf
//可能有多个满足的条件的子字符串,所以返回String数组
public List<String> getBigString(String s1,String s2){
String bigStr = (s1.length()>s2.length()) ? s1 : s2;
String smallStr = (s1.length()>s2.length()) ? s2 : s1;
List<String> resList = new ArrayList<>();
int resLen = 0;
List<String> list = allSonStr(smallStr);
for (String s : list) {
if(resLen > s.length()){
break;
}
if (bigStr.indexOf(s) != -1) {
System.out.println("最大子串为:"+ s);
resList.add(s);
resLen = s.length();
}
}
return resList;
}
public List<String> allSonStr(String s){
List<String> list= new ArrayList<>();
for (int i = 0; i <= s.length()-1; i++) {
for (int j = 0; j <= i; j++) {
list.add(s.substring(j,s.length()-i+j));
}
}
list = list.stream().distinct().collect(Collectors.toList());//去重
return list;
}
02-13
531
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)
08-22
1095
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)
11-07