用于n个字符串的最长公共子字符串的Java实现 [英] Java implementation for longest common substring of n strings

查看:119
本文介绍了用于n个字符串的最长公共子字符串的Java实现的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要找到n个字符串中最长的公共子字符串,并在我的项目中使用结果。

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

java中是否存在已经执行此操作的现有实现/库?

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

感谢你提前回复。

推荐答案

怎么样并发树

它是 Maven Central 中的一个小型(~100 KB)库。该算法使用 Radix 后缀树的组合。已知具有线性时间复杂度维基百科) 。

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();
}

这篇关于用于n个字符串的最长公共子字符串的Java实现的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆