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

2021/4/20 10:27:15

本文主要是介绍查找字符串中最长重复字符的子串,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

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



这篇关于查找字符串中最长重复字符的子串的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程