Java String Distance editDistance(String s, String t)

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

Description

Calculates the edit distance (aka Levenshtein distance) for two strings, i.e.

License

Apache License

Declaration

public static int editDistance(String s, String t) 

Method Source Code

//package com.java2s;
/*-------------------------------------------------------------------------+
|                                                                          |
| Copyright 2005-2011 The ConQAT Project                                   |
|                                                                          |
| Licensed under the Apache License, Version 2.0 (the "License");          |
| you may not use this file except in compliance with the License.         |
| You may obtain a copy of the License at                                  |
|                                                                          |
|    http://www.apache.org/licenses/LICENSE-2.0                            |
|                                                                          |
| Unless required by applicable law or agreed to in writing, software      |
| distributed under the License is distributed on an "AS IS" BASIS,        |
| WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| See the License for the specific language governing permissions and      |
| limitations under the License.                                           |
+-------------------------------------------------------------------------*/

public class Main {
    /**/*from   w ww.ja  v a  2  s.co m*/
     * Calculates the edit distance (aka Levenshtein distance) for two strings,
     * i.e. the number of insert, delete or replace operations required to
     * transform one string into the other. The running time is O(n*m) and the
     * space complexity is O(n+m), where n/m are the lengths of the strings.
     * Note that due to the high running time, for long strings the {@link Diff}
     * class should be used, that has a more efficient algorithm, but only for
     * insert/delete (not replace operation).
     * 
     * Although this is a clean reimplementation, the basic algorithm is
     * explained here: http://en.wikipedia.org/wiki/Levenshtein_distance#
     * Iterative_with_two_matrix_rows
     */
    public static int editDistance(String s, String t) {
        char[] sChars = s.toCharArray();
        char[] tChars = t.toCharArray();
        int m = s.length();
        int n = t.length();

        int[] distance = new int[m + 1];
        for (int i = 0; i <= m; ++i) {
            distance[i] = i;
        }

        int[] oldDistance = new int[m + 1];
        for (int j = 1; j <= n; ++j) {

            // swap distance and oldDistance
            int[] tmp = oldDistance;
            oldDistance = distance;
            distance = tmp;

            distance[0] = j;
            for (int i = 1; i <= m; ++i) {
                int cost = 1 + Math.min(distance[i - 1], oldDistance[i]);
                if (sChars[i - 1] == tChars[j - 1]) {
                    cost = Math.min(cost, oldDistance[i - 1]);
                } else {
                    cost = Math.min(cost, 1 + oldDistance[i - 1]);
                }
                distance[i] = cost;
            }
        }

        return distance[m];
    }
}

Related

  1. editDistance(CharSequence first, CharSequence second)
  2. editDistance(String one, String two)
  3. editDistance(String s, String t)
  4. editDistance(String s, String t)
  5. editDistance(String s0, String s1)
  6. EditDistance(String s1, String s2)
  7. editDistance(String str1, String str2)