Skip to content

Instantly share code, notes, and snippets.

@StoneCypher
Created February 15, 2014 09:22
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 StoneCypher/9016629 to your computer and use it in GitHub Desktop.
Save StoneCypher/9016629 to your computer and use it in GitHub Desktop.
Javascript Levenshtein
function levenshtein (s, t) {
if (!s.length) { return t.length; }
if (!t.length) { return s.length; }
return Math.min(
levenshtein(s.substr(1), t) + 1,
levenshtein(t.substr(1), s) + 1,
levenshtein(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