import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;

public class LongestCommonSubstring {
  private static String readFileAsString(String filePath) {
    File f = new File(filePath);
    BufferedReader reader = null;
    StringBuilder text = new StringBuilder(470000);
    String line = null;

    try {
      reader = new BufferedReader(new FileReader(f));
      while ((line = reader.readLine()) != null) {
        text.append(line);
      }
    } catch (FileNotFoundException e) {
      e.printStackTrace();
    } catch (IOException e) {
      e.printStackTrace();
    } finally {
      try {
        if (reader != null) {
          reader.close();
        }
      } catch (IOException e) {
      }
    }

    return text.toString();
  }

  private static String lcs(String s, String t) {
    int sLength = s.length();

    String text = s + t;
    int length = text.length();

    SuffixArray suffixes = new SuffixArray(text);

    String lcs = "";
    for (int i = 1; i < length; i++) {

      if (suffixes.indexOf(i) < sLength && suffixes.indexOf(i-1) < sLength) {
        continue;
      }

      if (suffixes.indexOf(i) > sLength && suffixes.indexOf(i-1) > sLength) {
        continue;
      }

      int _length = suffixes.longestCommonPreffix(i);
      if (_length > lcs.length()) {
        int index = suffixes.indexOf(i);
        lcs = text.substring(index, index + _length);
      }
    }

    return lcs;
  }

  public static void main(String[] args) {
    String s = readFileAsString("./data/brave_new_world.txt");
    String t = readFileAsString("./data/the_doors_of_perception.txt");

    System.out.println("The longest common substring is: '" + lcs(s, t) + "'");
  }
}

@@ -143,45 +143,368 @@
 n;%0A%0A
-public class LongestCommonSubstring %7B
+/**%0A * SuffixArray client that computes the longest common strung that appears in%0A * the two texts.%0A *%0A * TODO: this could be changed so that the files are given as an argument%0A */%0Apublic class LongestCommonSubstring %7B%0A%0A  /**%0A   * Reads the contents of the specified file and returns them as a string.%0A   *%0A   * @param filePath the path to the input file%0A   */
 %0A  p
@@ -1152,16 +1152,97 @@
 );%0A  %7D%0A%0A
+  /**%0A   * Returns the longest common string of the two specified strings.%0A   */%0A
   privat
@@ -1419,24 +1419,67 @@
 ray(text);%0A%0A
+    // Search for longest common substring%0A
     String l
@@ -1527,16 +1527,61 @@
 i++) %7B%0A%0A
+      // Adjacent suffixes from first string%0A
       if
@@ -1673,24 +1673,70 @@
 e;%0A      %7D%0A%0A
+      // Adjacent suffixes from second string%0A
       if (su
@@ -1824,24 +1824,86 @@
 e;%0A      %7D%0A%0A
+      // Are adjancent suffixes longer than common substring?%0A
       int _l

Add documentation