/** * @author weichen CHEN created on 2018/4/20 * @version 2018/4/20 weichen CHEN */ public class Test { public static void main(String[] args) { String a = "abcabcabca"; String b = "ababcacccbcab"; char[] x = a.toCharArray(); char[] y = b.toCharArray(); int count = 0; int maxlength = 0; int start = 0; for (int k = 1; k <= y.length; k++){ for (int i = 0; i < x.length-k+1; i++) { for (int j = 0; j < y.length - k + 1; j++) { count=0; for (int l = 0; l < k; l++) if (y[j + l] == x[i + l]) count++; if (count == k && k > maxlength) { maxlength = k; start = j; } } } } if(maxlength==0) System.out.println("no"); else for(int i=0; i<maxlength; i++) System.out.println(y[start+i]); } }