2012-11-03 30 views
-2

可能重複:
How to find the longest repeated substring of given string一個發現最長的重複子

我想找到一個字符串的最長的重複子。

/** 
This method will find the longest substring of a given string. 
String given here is reassuring. 

*/ 
public String longestRepeatedSubstring() 
{ 
    String longestRepeatedSubstring = ""; 
    for (int i = 0; i<text.length(); i++) 
    { 
     String one = text.substring(0,i); 

     for(int o = 0; o<text.length();o++) 
     { 
      Sting two = text.substring(0,o); 
      if(one.equals(two)) 
      { 
       longestRepeatedSubstring = one; 
      } 

     } 

    } 
    return longestRepeatedSubstring; 
} 
+2

你的問題是什麼? –

+0

如果您的問題沒有得到解答,請嘗試解決評論,不要再發布。 –

+0

我無法弄清楚我的代碼有什麼問題。我正在嘗試查找字符串的最長子字符串。 –

回答

0

你最好使用正則表達式。這裏是:

/(?=((.+)(?:.*?\2)+))/s 
+0

你是什麼意思 –