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

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

Description

Derived from pseudocode at: http://en.wikipedia.org/wiki/Levenshtein_distance Used to determine edit distances between two strings

License

Open Source License

Declaration


public static int levenshteinDistance(String s, String t) 

Method Source Code

//package com.java2s;
/*/*from  ww  w. j a  v  a2 s  . co  m*/
**    Copyright (C) 2003-2018 Institute for Systems Biology 
**                            Seattle, Washington, USA. 
**
**    This library is free software; you can redistribute it and/or
**    modify it under the terms of the GNU Lesser General Public
**    License as published by the Free Software Foundation; either
**    version 2.1 of the License, or (at your option) any later version.
**
**    This library is distributed in the hope that it will be useful,
**    but WITHOUT ANY WARRANTY; without even the implied warranty of
**    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
**    Lesser General Public License for more details.
**
**    You should have received a copy of the GNU Lesser General Public
**    License along with this library; if not, write to the Free Software
**    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
*/

public class Main {
    /***************************************************************************
    **
    ** Derived from pseudocode at: http://en.wikipedia.org/wiki/Levenshtein_distance
    ** Used to determine edit distances between two strings
    */

    public static int levenshteinDistance(String s, String t) {

        int m = s.length();
        int n = t.length();

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

        for (int i = 0; i <= m; i++) {
            dtable[i][0] = i; // deletion
        }

        for (int j = 0; j <= n; j++) {
            dtable[0][j] = j; // insertion
        }

        for (int j = 1; j <= n; j++) {
            for (int i = 1; i <= m; i++) {
                if (s.charAt(i - 1) == t.charAt(j - 1)) {
                    dtable[i][j] = dtable[i - 1][j - 1];
                } else {
                    int delDist = dtable[i - 1][j] + 1; // deletion
                    int insDist = dtable[i][j - 1] + 1; // insertion
                    int subDist = dtable[i - 1][j - 1] + 1; // substitution
                    dtable[i][j] = Math.min(Math.min(delDist, insDist), subDist);
                }
            }
        }

        return (dtable[m][n]);
    }
}

Related

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