Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java implementation for longest common substring of n strings

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?

like image 685
user1628340 Avatar asked Dec 03 '22 22:12

user1628340


1 Answers

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();
}
like image 142
dedek Avatar answered Jan 18 '23 23:01

dedek