Java String Levenshtein Distance levenshteinDistance(String[] a, String[] b)

Here you can find the source of levenshteinDistance(String[] a, String[] b)

Description

Straight from Wikipedia

License

Apache License

Declaration

private static int levenshteinDistance(String[] a, String[] b) 

Method Source Code

//package com.java2s;
//License from project: Apache License 

public class Main {
    /**/*from  w w w  .j  av a  2 s . c om*/
     * Straight from Wikipedia
     */
    private static int levenshteinDistance(String[] a, String[] b) {
        int[][] distance = new int[a.length + 1][b.length + 1];

        for (int i = 0; i <= a.length; i++) {
            distance[i][0] = i;
        }
        for (int j = 1; j <= b.length; j++) {
            distance[0][j] = j;
        }

        for (int i = 1; i <= a.length; i++) {
            for (int j = 1; j <= b.length; j++) {
                distance[i][j] = minimum(distance[i - 1][j] + 1, distance[i][j - 1] + 1,
                        distance[i - 1][j - 1] + ((a[i - 1].equals(b[j - 1])) ? 0 : 1));
            }
        }

        return distance[a.length][b.length];
    }

    private static int minimum(int a, int b, int c) {
        return Math.min(Math.min(a, b), c);
    }
}

Related

  1. levenshteinDistance(String s1, String s2)
  2. levenshteinDistance(String st1, String st2)
  3. levenshteinDistance(String string1, String string2)
  4. levenshteinDistance(String wordForm, String lemma)
  5. levenshteinDistance(String x, String y)
  6. levenshteinDistanceRatio(CharSequence lhs, CharSequence rhs)
  7. levenshteinEquals(double threshold, String dom1, String dom2)
  8. levenshteinSimilarity(String string1, String string2)