这是我自己写的代码,你看看。。
public class Test3 {
/**
* @param args
*/
public static void main(String[] args) {
String s1 = "wertabckkkyuioyyyy";
String s2 = "xcbabcnkkkn";
String max = getMaxSubstring(s2,s1);
System.out.println(max);
}
private static String getMaxSubstring(String s1, String s2) {
String min,max;
max = s1.length()>s2.length()?s1:s2;
min = max.equals(s1)?s2:s1;
System.out.println("maxlen:"+max);
System.out.println("minlen:"+min);
for (int i = 0; i < min.length(); i++) {
for (int start = 0,end = min.length()-i; end != min.length()+1;start++,end++) {
String temp = min.substring(start,end);
if(max.contains(temp))//if(s1.indexOf(temp)!=-1)
return temp;
// System.out.println(temp);
}
}
return null;
}
private static String getMaxSubstring2(String s1, String s2) {
String max = null;
w:for (int i = 0; i < s2.length(); i++) {
for (int start = 0,end = s2.length()-i; end != s2.length()+1;start++,end++) {
String temp = s2.substring(start,end);
if(s1.contains(temp))//if(s1.indexOf(temp)!=-1)
{
//return temp;
max = temp;
System.out.println(temp);
break w;
}
}
}
/**/
for(int x = 0,y = max.length(); y!=s2.length()+1 ; x++,y++ ){
String s = s2.substring(x, y);
//System.out.println(s+"--");
if(s1.contains(s))
System.out.println("max==="+s);
}
return null;
}
}
|