查找字符串中最长重复字符的子串

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");
}
发布了34 篇原创文章 · 获赞 2 · 访问量 4万+

猜你喜欢

转载自blog.csdn.net/zjj2006/article/details/42804557