title: Longest repeating and non-overlapping substring in a huge string
body: <p>I used this program to find the longest repeated substring in a string. I found it on this link(<a href="https://www.geeksforgeeks.org/longest-repeating-and-non-overlapping-substring/" rel="nofollow noreferrer">https://www.geeksforgeeks.org/longest-repeating-and-non-overlapping-substring/</a>). This program works perfectly if my input is a smaller string.</p>
  
  <pre><code>    static String longestRepeatedSubstring(String str) { 
          int n = str.length(); 
          int LCSRe[][] = new int[n + 1][n + 1]; 
  
          String res = ""; // To store result  
          int res_length = 0; // To store length of result  
  
          int i, index = 0; 
          for (i = 1; i &lt;= n; i++) { 
              for (int j = i + 1; j &lt;= n; j++) { 
                  if (str.charAt(i - 1) == str.charAt(j - 1) 
                          &amp;&amp; LCSRe[i - 1][j - 1] &lt; (j - i)) { 
                      LCSRe[i][j] = LCSRe[i - 1][j - 1] + 1; 
  
                      if (LCSRe[i][j] &gt; res_length) { 
                          res_length = LCSRe[i][j]; 
                          index = Math.max(i, index); 
                      } 
                  } else { 
                      LCSRe[i][j] = 0; 
                  } 
              } 
          } 
  
          if (res_length &gt; 0) { 
              for (i = index - res_length + 1; i &lt;= index; i++) { 
                  res += str.charAt(i - 1); 
              } 
          } 
  
          return res; 
      } 
  
      public static void main(String[] args) { 
          String str = "abracadabra"; 
          System.out.println(longestRepeatedSubstring(str)); 
      } 
  </code></pre>
  
  <p>However i have a string that has 14553776 characters and when i run the program i always get <code>java.lang.OutOfMemoryError: Java heap space</code>. I have tried increasing heap size and this thing with virtual memory.</p>
  
  <p>I wonder if its there any way to solve this problem. Maby improving the program or any other solution?</p>
  
  <p>Thanks for helping</p>