MCPcopy
hub / github.com/mgechev/javascript-algorithms / levenshteinDistance

Function levenshteinDistance

src/others/levenshtein-distance.js:6–31  ·  view source on GitHub ↗
(s, ls, t, lt)

Source from the content-addressed store, hash-verified

4 var levenshteinDistance = (function () {
5
6 function levenshteinDistance (s, ls, t, lt) {
7 var memo = [];
8 var currRowMemo;
9 var i;
10 var k;
11
12 for (k = 0; k <= lt; k += 1) {
13 memo[k] = k;
14 }
15
16 for (i = 1; i <= ls; i += 1) {
17 currRowMemo = [i];
18
19 for (k = 1; k <= lt; k += 1) {
20 currRowMemo[k] = Math.min(
21 currRowMemo[k - 1] + 1,
22 memo[k] + 1,
23 memo[k - 1] + (s[i - 1] !== t[k - 1] ? 1 : 0)
24 );
25 }
26
27 memo = currRowMemo;
28 }
29
30 return memo[lt];
31 }
32
33 /**
34 * The Levenshtein distance between two strings is a minimum number

Calls

no outgoing calls

Tested by

no test coverage detected