1String.prototype.levenstein = function(string) {
2 var a = this, b = string + "", m = [], i, j, min = Math.min;
3
4 if (!(a && b)) return (b || a).length;
5
6 for (i = 0; i <= b.length; m[i] = [i++]);
7 for (j = 0; j <= a.length; m[0][j] = j++);
8
9 for (i = 1; i <= b.length; i++) {
10 for (j = 1; j <= a.length; j++) {
11 m[i][j] = b.charAt(i - 1) == a.charAt(j - 1)
12 ? m[i - 1][j - 1]
13 : m[i][j] = min(
14 m[i - 1][j - 1] + 1,
15 min(m[i][j - 1] + 1, m[i - 1 ][j] + 1))
16 }
17 }
18
19 return m[b.length][a.length];
20}