Java String Levenshtein Distance levenshteinDistance(String s, String t)

Here you can find the source of levenshteinDistance(String s, String t)

Description

levenshtein Distance

License

Open Source License

Declaration

public static int levenshteinDistance(String s, String t) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

public class Main {
    public static int levenshteinDistance(String s, String t) {
        int n = s.length();
        int m = t.length();

        if (n == 0)
            return m;
        if (m == 0)
            return n;

        int[][] d = new int[n + 1][m + 1];

        for (int i = 0; i <= n; d[i][0] = i++)
            ;/*from   w  ww. ja v a  2 s. c o m*/
        for (int j = 1; j <= m; d[0][j] = j++)
            ;

        for (int i = 1; i <= n; i++) {
            char sc = s.charAt(i - 1);
            for (int j = 1; j <= m; j++) {
                int v = d[i - 1][j - 1];
                if (t.charAt(j - 1) != sc)
                    v++;
                d[i][j] = Math.min(Math.min(d[i - 1][j] + 1, d[i][j - 1] + 1), v);
            }
        }
        return d[n][m];
    }
}

Related

  1. levenshteinDistance(CharSequence lhs, CharSequence rhs)
  2. levenshteinDistance(CharSequence s, CharSequence t)
  3. levenshteinDistance(CharSequence str1, CharSequence str2)
  4. levenshteinDistance(final String string1, final String string2, int swap, int substitution, int insertion, int deletion)
  5. LevenshteinDistance(String actual, String expected)
  6. levenshteinDistance(String s, String t)
  7. LevenshteinDistance(String s, String t)
  8. levenshteinDistance(String s, String t)
  9. levenshteinDistance(String s, String t)