Java implementation for longest common substring of n strings

user1628340 picture user1628340 · Jun 17, 2013 · Viewed 11.3k times · Source

I need to find the longest common substring of n strings and use the result in my project.

Is there any existing implementation/library in java which already does this?

Answer

dedek picture dedek · Mar 7, 2015

What about concurrent-trees ?

It is a small (~100 KB) library available in Maven Central. The algorithm uses combination of Radix and Suffix Trees. Which is known to have a linear time complexity (wikipedia).

public static String getLongestCommonSubstring(Collection<String> strings) {
    LCSubstringSolver solver = new LCSubstringSolver(new DefaultCharSequenceNodeFactory());
    for (String s: strings) {
        solver.add(s);
    }
    return solver.getLongestCommonSubstring().toString();
}