EditDistance { public static int levenshtein(@NotNull CharSequence str1, @NotNull CharSequence str2, boolean caseSensitive) { int[][] d = prepare(str1.length(), str2.length()); for (int i = 1; i <= str1.length(); i++) { for (int j = 1; j <= str2.length(); j++) { int cost = equal(str1.charAt(i - 1), str2.charAt(j - 1), caseSensitive) ? 0 : 1; d[i][j] = min(d[i - 1][j] + 1, d[i][j - 1] + 1, d[i - 1][j - 1] + cost); } } return d[str1.length()][str2.length()]; } private  EditDistance(); static int levenshtein(@NotNull CharSequence str1, @NotNull CharSequence str2, boolean caseSensitive); static int optimalAlignment(@NotNull CharSequence str1, @NotNull CharSequence str2, boolean caseSensitive);  }
@Test public void levenshtein() { assertEquals(0, EditDistance.levenshtein("", "", true)); assertEquals(0, EditDistance.levenshtein("AA", "AA", true)); assertEquals(1, EditDistance.levenshtein("AA", "Aa", true)); assertEquals(2, EditDistance.levenshtein("AA", "aa", true)); assertEquals(2, EditDistance.levenshtein("ab", "ba", true)); }
@Test public void levenshteinCaseInsensitive() { assertEquals(0, EditDistance.levenshtein("", "", false)); assertEquals(0, EditDistance.levenshtein("AA", "AA", false)); assertEquals(0, EditDistance.levenshtein("AA", "Aa", false)); assertEquals(0, EditDistance.levenshtein("AA", "aa", false)); assertEquals(2, EditDistance.levenshtein("ab", "ba", false)); }