Sting类中最大相同子串

public class stringtest2 {
	public static void main(String[] args){
		
		String s1 = "assabcdodijdnj";
		String s2 = "dsabcdokj";
		String s = method(s1,s2);
		System.out.println("MaxSubString:"+s);
	}
	
	public static String method(String s1, String s2){
		
		for (int i = 0; i < s2.length(); i++) {
			for (int x =0,y=s2.length()-i;y!=s2.length()+1;x++,y++){
				String sub = s2.substring(x, y);
				if(s1.contains(sub)){
					return sub;
				}
			}
		}
		return null;
	}
}
中间还可以加个判断,s1,s2谁长。

你可能感兴趣的:(java,String)