-
Notifications
You must be signed in to change notification settings - Fork 0
/
levenshteinDistance.js
37 lines (36 loc) · 1018 Bytes
/
levenshteinDistance.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
function levenshteinDistance(str1 = '', str2 = '') {
const track = Array(str2.length + 1).fill(null).map(() =>
Array(str1.length + 1).fill(null));
for (let i = 0; i <= str1.length; i += 1) {
track[0][i] = i;
}
for (let j = 0; j <= str2.length; j += 1) {
track[j][0] = j;
}
for (let j = 1; j <= str2.length; j += 1) {
for (let i = 1; i <= str1.length; i += 1) {
const indicator = str1[i - 1] === str2[j - 1] ? 0 : 1;
track[j][i] = Math.min(
track[j][i - 1] + 1, // deletion
track[j - 1][i] + 1, // insertion
track[j - 1][i - 1] + indicator, // substitution
);
}
}
return track[str2.length][str1.length];
};
function getClosestOption(str, array) {
if (array.includes(str)) {
return str;
}
let minDistance = Infinity;
let option = str;
array.forEach(str2 => {
const distance = levenshteinDistance(str, str2);
if (distance < minDistance) {
minDistance = distance;
option = str2;
}
});
return option;
}