Skip to content

Instantly share code, notes, and snippets.

@santhoshtr
Forked from IgorInger/levenshtein.js
Created August 10, 2012 08:59
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save santhoshtr/3312717 to your computer and use it in GitHub Desktop.
Save santhoshtr/3312717 to your computer and use it in GitHub Desktop.
Levenshtein distance- Javascript
var levenshteinDistance = function(u, v) {
var m = u.length;
var n = v.length;
var D = [];
for(var i = 0; i <= m; i++) {
D.push([]);
for(var j = 0; j <= n; j++) {
D[i][j] = 0;
}
}
for(var i = 1; i <= m; i++) {
for(var j = 1; j <= n; j++) {
if (j == 0) {
D[i][j] = i;
} else if (i == 0) {
D[i][j] = j;
} else {
D[i][j] = [D[i-1][j-1] + (u[i-1] != v[j-1]), D[i][j-1] + 1, D[i-1][j] + 1].sort()[0];
}
}
}
return D[m][n];
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment