temppos:记录子字符串开始的下标
list:存放重复的子字符串
public class RepeatString {
private static void longestdupString(String s) {
if (s == null || s.length() == 0) {
return;
}
char temp = s.charAt(0);
int temppos = 0;
List<String> list = new ArrayList<String>();
for (int i = 1; i < s.length(); i++) {
if (i == s.length() -1) {
if (list.size() == 0) {
list.add(s);
break;
}else {
if ((i+1 - temppos) > list.get(0).length()) {
list.clear();
list.add(s.substring(temppos, i+1));
}else if((i+1 - temppos) == list.get(0).length()){
list.add(s.substring(temppos, i+1));
}else {
}
}
}
if (s.charAt(i) == temp) {
continue;
}
if (list.size() == 0) {
list.add(s.substring(temppos, i));
} else {
if ((i - temppos) > list.get(0).length()) {
list.clear();
list.add(s.substring(temppos, i));
}else if((i - temppos) == list.get(0).length()){
list.add(s.substring(temppos, i));
}else {
}
}
temp = s.charAt(i);
temppos = i;
}
for (int i = 0; i < list.size(); i++) {
System.out.println(list.get(i));
}
}
public static void main(String[] args){
longestdupString("aaaaabbcaadddddseefgfsggggsssss");
}