码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2015-01-17 13:57:37      阅读:106      评论:0      收藏:0      [点我收藏+]

标签:字符串   最长   重复字符   

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");
}

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

标签:字符串   最长   重复字符   

原文地址:http://blog.csdn.net/zjj2006/article/details/42804557

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!