Java String Distance EditDistance(String s1, String s2)

Here you can find the source of EditDistance(String s1, String s2)

Description

Levenshtein Distance Taken from an implementation during an interview :p

License

Open Source License

Return

The edit distance

Declaration

public static int EditDistance(String s1, String s2) 

Method Source Code

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

public class Main {
    /**/*from  www.j a va 2  s  .c  o  m*/
     * Levenshtein Distance
     * Taken from an implementation during an interview :p
     *
     * @return The edit distance
     */
    public static int EditDistance(String s1, String s2) {
        if (s1.length() == 0) {
            return s2.length();
        }
        if (s2.length() == 0) {
            return s1.length();
        }
        int edit = (s1.charAt(0) == s2.charAt(0)) ? 0 : 1;

        int add = EditDistance(s1.substring(1), s2) + 1;
        int delete = EditDistance(s1, s2.substring(1)) + 1;
        int change = EditDistance(s1.substring(1), s2.substring(1)) + edit;
        return Math.min(change, Math.min(add, delete));
    }
}

Related

  1. editDistance(String one, String two)
  2. editDistance(String s, String t)
  3. editDistance(String s, String t)
  4. editDistance(String s, String t)
  5. editDistance(String s0, String s1)
  6. editDistance(String str1, String str2)
  7. editDistance(String string1, String string2)
  8. editDistance(String word1, String word2)
  9. Sqr(String[] X)