Skip to content

Instantly share code, notes, and snippets.

@amoilanen
Created October 18, 2014 12:05
Show Gist options
  • Save amoilanen/b2364b263ff376582306 to your computer and use it in GitHub Desktop.
Save amoilanen/b2364b263ff376582306 to your computer and use it in GitHub Desktop.
Computes Levenshtein distance between two strings http://en.wikipedia.org/wiki/Levenshtein_distance
/*
* Computing the Levenstein distance between strings.
* http://en.wikipedia.org/wiki/Levenshtein_distance
*/
/*
* Inefficient recursive algorithm.
*/
function stringDistance(str1, length1, str2, length2) {
/*
* If one of the strings is empty then the distance
* is the length of the other string.
*/
if ((length1 == 0) || (length2 == 0)) {
return length1 + length2;
}
var cost = (str1[length1 - 1] == str2[length2 - 1]) ? 0 : 1;
/*
* Looking at all the possible options to transform one string into another
* and choosing the minimum number of steps which will be by definition
* the Levenshtein distance between str1 and str2:
* - Removing one character from first string
* - Removing one character from second string
* - Removing one character from both strings
*/
return Math.min(
stringDistance(str1, length1 - 1, str2, length2) + 1,
stringDistance(str1, length1, str2, length2 - 1) + 1,
stringDistance(str1, length1 - 1, str2, length2 - 1) + cost
);
}
function distance(str1, str2) {
return stringDistance(str1, str1.length, str2, str2.length);
}
console.log("String distance = ", distance("kitten", "sitting"));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment