Java String Levenshtein Distance levenshteinDistance(CharSequence lhs, CharSequence rhs)

Here you can find the source of levenshteinDistance(CharSequence lhs, CharSequence rhs)

Description

levenshtein Distance

License

Open Source License

Declaration

public static int levenshteinDistance(CharSequence lhs, CharSequence rhs) 

Method Source Code

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

public class Main {
    public static int levenshteinDistance(CharSequence lhs, CharSequence rhs) {
        int[][] distance = new int[lhs.length() + 1][rhs.length() + 1];

        for (int i = 0; i <= lhs.length(); i++)
            distance[i][0] = i;//w  w w. j  a  va  2  s .  c o  m
        for (int j = 1; j <= rhs.length(); j++)
            distance[0][j] = j;

        for (int i = 1; i <= lhs.length(); i++)
            for (int j = 1; j <= rhs.length(); j++)
                distance[i][j] = Math.min(Math.min(distance[i - 1][j] + 1, distance[i][j - 1] + 1),
                        distance[i - 1][j - 1] + ((lhs.charAt(i - 1) == rhs.charAt(j - 1)) ? 0 : 1));

        return distance[lhs.length()][rhs.length()];
    }
}

Related

  1. levenshtein(final String s1, final String s2)
  2. levenshtein(String s, String t)
  3. levenshtein(String str1, String str2)
  4. levenshteinDistance(CharSequence lhs, CharSequence rhs)
  5. levenshteinDistance(CharSequence s, CharSequence t)
  6. levenshteinDistance(CharSequence str1, CharSequence str2)
  7. levenshteinDistance(final String string1, final String string2, int swap, int substitution, int insertion, int deletion)