* 给定一个字符串,返回里面有重复出现的且长度最长的子串 * 如字符串"abchepbchepabc",里面最长的重复字符串是"bchep"
public void getMaxLenStr(String str) { if (null == str || 0 == str.length()) { return; } int k = 0; int max = 0; int first = 0; for (int i = 1; i < str.length(); i++) { //从当前位置开始,将相隔i 的重复的求出来! for (int j = 0; j < str.length() - i; j++) { if (str.charAt(j) == str.charAt(i + j)) { k++; } else { k = 0; } if (k > max) { max = k; first = j - k + 1;//实质为:j-(k-1) } } k = 0; } System.out.println("最长的子串为" + str.substring(first, first + max)); }