使用java,输入string =“aabbcdeaaaabbb”,输出必须是aaaa

时间:2016-07-28 10:18:33

标签: java

使用java,输入string =“aabbcdeaaaabbb”,输出必须是aaaa,因为这里的序列重复了4次a。任何人都可以帮助我使用java实现将这个“aaaa”作为输出。

找到重复相同字符的最长子字符串的算法。

例如:

I / P:aabbcdefaaaacccccc O / P:cccccc

请检查下面的程序并建议任何优化以加快处理速度:

public class LongestSubString {
public static void main(String[] args) throws IOException {

BufferedReader reader = new BufferedReader(new InputStreamReader(
        System.in));
System.out
        .println("Enter a word to find longest substring with same characters repeated");
String word = reader.readLine();

System.out.println("Entered word is: " + word);

System.out.println("Longest repeated characters substring is: "
        + subStringFinder(word));

}

/*
*longest substring finder with same character repeated
*/

public static String subStringFinder(String word) {
char[] tokens = word.toCharArray();
int len = tokens.length;
int wordLen = word.length();

System.out.println("len of input word: " + wordLen);

List<String> myList = new ArrayList<>();
StringBuilder strConcat = new StringBuilder("");
for (int j = 0; j <= len - 1; j++) {

    if (j + 1 > len - 1) {
        if ((strConcat.length() >= 1)
                && (strConcat.charAt(strConcat.length() - 1) == (tokens[j]))) {
            strConcat.append("" + tokens[j]);
            myList.add(strConcat.toString());
        }
    }

    else {
        if (tokens[j] == tokens[j + 1]) {
            if ((strConcat.length() >= 1)
                    && (strConcat.charAt(strConcat.length() - 1) == (tokens[j]))) {
                strConcat.append("" + tokens[j]);
                myList.add(strConcat.toString());
            } else {
                strConcat = new StringBuilder("");
                strConcat.append("" + tokens[j]);
            }
        } else {
            if ((strConcat.length() >= 1)
                    && (strConcat.charAt(strConcat.length() - 1) == (tokens[j]))) {
                strConcat.append("" + tokens[j]);
                myList.add(strConcat.toString());
            } else {
                strConcat = new StringBuilder("");
                strConcat.append("" + tokens[j]);
            }
        }
    }
}

int max = 0, index = 0;
for (int i = 0; i < myList.size(); i++) {
    String strEle = myList.get(i);
    int strLen = strEle.length();

    if (max < strLen) {
        max = strLen;
        index = i;
    }

}

return myList.get(index);

}
}

2 个答案:

答案 0 :(得分:1)

我相信你的代码过于复杂。您不需要StringBuilder也不需要ArrayList。我试图理解你的意图,但后来跳过它并编写了我自己的版本。希望它无论如何都有帮助。

public static String subStringFinder(String word) {
    if (word == null || word.isEmpty()) {
        return word;
    }
    char currentChar = word.charAt(0);
    int longestStart = 0;
    int longestLength = 0;
    int currentStart = 0;
    int currentLength = 1;
    for (int ix = 1; ix < word.length(); ix++) {
        if (word.charAt(ix) == currentChar) {
            currentLength++;
        } else {
            if (currentLength > longestLength) {
                longestStart = currentStart;
                longestLength = currentLength;
            }
            currentChar = word.charAt(ix);
            currentStart = ix;
            currentLength = 1;
        }
    }
    if (currentLength > longestLength) {
        longestStart = currentStart;
        longestLength = currentLength;
    }
    return word.substring(longestStart, longestStart + longestLength);
}

答案 1 :(得分:0)

String in = "aabbcdeaaaabbb"; 
String t; 
ArrayList<String> out = new ArrayList<String>(); 
String l="";
int c=0; 
String n; 

  for(int i=0;i<in.length;i++) { 
     n=in.substring(i, i+1); //get the current character 
     if(n.equals(l)){ 
        l=n; c++; 
      } 
    else { 
       t=n; 
       for(int j=1;j<c;j++) { 
           n+=t;
        }
    c=0; 
    out.add(n); 
    c=0; 
  } 
}