Skip to content

Instantly share code, notes, and snippets.

@mohsen1
Created October 14, 2012 11:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mohsen1/3888271 to your computer and use it in GitHub Desktop.
Save mohsen1/3888271 to your computer and use it in GitHub Desktop.
Levenshtein Distance calculator
function levenshteinDistance (s, t) {
if (!s.length) return t.length;
if (!t.length) return s.length;
return Math.min(
levenshteinDistance(s.substr(1), t) + 1,
levenshteinDistance(t.substr(1), s) + 1,
levenshteinDistance(s.substr(1), t.substr(1)) + (s[0] !== t[0] ? 1 : 0)
);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment