Find The Longest Repeated Substring

This recipe shows how to find the longest repeated substring in a string.

Problem:

Given an input string, find the longest substring that occurs at least twice in the string.

Solution:

This solution uses a suffix array to solve the problem.

public class LongestRepeatedSubstring {

    public static String lrs(String input) {
        // form suffix array
        final int N = input.length();
        final String[] suffixes = new String[N];
        for (int i = 0; i < N; i++) {
            suffixes[i] = input.substring(i, N);
        }

        // sort the suffix array in ascending order
        MergeSort.sort(suffixes);

        // find LCP (longest common prefix) among adjacent entries
        String lrs = "";
        for (int i = 0; i < N - 1; i++) {
            final String lcp = LongestCommonPrefix.lcp(suffixes[i], suffixes[i + 1]);
            if (lcp.length() > lrs.length()) {
                lrs = lcp;
            }
        }
        return lrs;
    }

    public static void main(String[] args) {
        final String input = args[0];
        System.out.println(lrs(input));
    }
}

Link To: Java Source Code