寻找最长的重复子串

时间:2012-11-03 19:00:01

标签: java string

  

可能重复:
  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; 
}

1 个答案:

答案 0 :(得分:0)

你最好使用正则表达式。这是:

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