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?
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();
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With