您提到使用Levenhstein 的编辑距离,并且您的字符串长约 3400 个字符。
我做了一个快速的尝试,并使用 Levenhstein 的编辑距离的动态编程版本,它似乎非常快并且不会引起任何问题。
我这样做了:
final StringBuilder sb1 = new StringBuilder();
final StringBuilder sb2 = new StringBuilder();
final Random r = new Random(42);
final int n = 3400;
for (int i = 0; i < n; i++) {
sb1.append( (char) ('a' + r.nextInt(26)) );
sb2.append( (char) ('a' + r.nextInt(26)) );
}
final long t0 = System.currentTimeMillis();
System.out.println("LED: " + getLevenshteinDistance(sb1.toString(), sb2.toString()) );
final long te = System.currentTimeMillis() - t0;
System.out.println("Took: " + te + " ms");
它在 2006 年左右的 Core 2 Duo 上找到了 215 毫秒的距离。
这对你有用吗?
(btw I'm not sure I can paste the code for the DP LED implementation I've got here so you probably should search the Internet for one Java implementation)