2016-07-28 46 views
-7

使用java,輸入string =「aabbcdeaaaabbb」並且輸出必須是aaaa,因爲這裏的序列重複4次a。任何人都可以幫助我使用java實現獲得這個「aaaa」作爲輸出。使用java,輸入字符串=「aabbcdeaaaabbb」,輸出必須是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); 

} 
} 
+0

發佈您的代碼! –

+2

「aabbcdeaaaabbb」.substring(7,11) – ekaerovets

+0

@ekaerovets - 這只是我提供的一般示例字符串。我的問題是,在一個字符串中找到只有重複字符的最長的子字符串,在這種情況下,它的「aaaa」。 – Arafath

回答

0

我相信你的代碼太複雜了。您不需要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); 
} 
+0

謝謝,你的代碼邏輯很好:) – Arafath

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; 
    } 
} 
+0

我已經從答案的底部刪除了無用的文本,並接受了您的重新格式化。你可以添加一些關於OP的問題的解釋,以及你的代碼如何修復它? –