title: How to find a longest consecutive repeated substring in a string?
body: <p>In order to de-crypt a message, I need to first find the key. From the given information, I find the key is part of the string:</p>
  
  <blockquote>
    <p>str = "251220825122082"</p>
  </blockquote>
  
  <p>We can easily get that the key should be "<code>2512208</code>" since the key is supposed to be repeatedly used in encrypting a message. However, I tried many methods and got the answer "<code>25122082</code>", which adds another 2 in the end, but it's just another beginning of the key.</p>
  
  <p>The method I have tried:</p>
  
  <ol>
  <li>regex: <code>String repeated = str.replaceAll("(.+?)\\1+", "$1");</code></li>
  <li><a href="http://introcs.cs.princeton.edu/java/42sort/LRS.java.html" rel="nofollow noreferrer">LRS Java</a></li>
  </ol>
  
  <p>These two provide the same answer("<code>25122082</code>").</p>
  
  <p>Can anyone help me with this problem?</p>
  
  <p>Thank you!</p>