public class StringTest3 {
public static void main(String[] args) {
String str1 = "helabchaahha";
String str2 = "boodoabcdworldhhha";
String str3 = getMaxStr(str1,str2);
System.out.println(str3);
}
public static String getMaxStr(String s1,String s2) {
String min=null,max=null;
max = (s1.length()>s2.length())?s1:s2;
min = (max.equals(s1))?s2:s1;
for(int x=0;x<min.length();x++) {
for(int m=0,n=min.length()-x;n!=min.length()+1;m++,n++) {
String s3 = max.substring(m, n);
//System.out.println(s3);
if(s1.contains(s3)) {
return max+" "+min+"\n"+"最大的子串为"+s3;
}
}
}
return null;
}
}
时间: 2024-07-31 17:07:04