snippetjavascriptTip
Calculate the Levenshtein distance between two strings in JavaScript
Viewed 0 times
distancejavascripttwolevenshteinbetweenstringscalculatethe
Problem
The Levenshtein distance is a measure of the difference between two strings. It is defined as the minimum number of single-character edits (insertions, deletions or substitutions) required to change one string into the other. It is also sometimes referred to as _edit distance_, although that may also refer to different distance metrics.
The following implementation is based on the Wagner–Fischer algorithm. It uses a 2D array to store the distances between all prefixes of the two strings. The last element of the last row of the array contains the Levenshtein distance between the two strings.
The following implementation is based on the Wagner–Fischer algorithm. It uses a 2D array to store the distances between all prefixes of the two strings. The last element of the last row of the array contains the Levenshtein distance between the two strings.
- Start by checking the
lengthof each string. If either of them is zero, return thelengthof the other one. - Create an empty 2D array. Use a
forloop to iterate over the letters of the target string and a nestedforloop to iterate over the letters of the source string. - Calculate the cost of substitution for the letters corresponding to
i - 1andj - 1in the target and source respectively (0if they are the same,1otherwise).
Solution
const levenshteinDistance = (s, t) => {
if (!s.length) return t.length;
if (!t.length) return s.length;
const arr = [];
for (let i = 0; i <= t.length; i++) {
arr[i] = [i];
for (let j = 1; j <= s.length; j++) {
arr[i][j] =
i === 0
? j
: Math.min(
arr[i - 1][j] + 1,
arr[i][j - 1] + 1,
arr[i - 1][j - 1] + (s[j - 1] === t[i - 1] ? 0 : 1)
);
}
}
return arr[t.length][s.length];
};
levenshteinDistance('duck', 'dark'); // 2
levenshteinDistance('foo', 'foobar'); // 3- Start by checking the
lengthof each string. If either of them is zero, return thelengthof the other one. - Create an empty 2D array. Use a
forloop to iterate over the letters of the target string and a nestedforloop to iterate over the letters of the source string. - Calculate the cost of substitution for the letters corresponding to
i - 1andj - 1in the target and source respectively (0if they are the same,1otherwise). - Use
Math.min()to populate each element in the 2D array with the minimum of the cell above incremented by one, the cell to the left incremented by one or the cell to the top left incremented by the previously calculated cost. - Return the last element of the last row of the produced array. This is the Levenshtein distance between the two strings.
@Further reading
Code Snippets
const levenshteinDistance = (s, t) => {
if (!s.length) return t.length;
if (!t.length) return s.length;
const arr = [];
for (let i = 0; i <= t.length; i++) {
arr[i] = [i];
for (let j = 1; j <= s.length; j++) {
arr[i][j] =
i === 0
? j
: Math.min(
arr[i - 1][j] + 1,
arr[i][j - 1] + 1,
arr[i - 1][j - 1] + (s[j - 1] === t[i - 1] ? 0 : 1)
);
}
}
return arr[t.length][s.length];
};
levenshteinDistance('duck', 'dark'); // 2
levenshteinDistance('foo', 'foobar'); // 3Context
From 30-seconds-of-code: levenshtein-distance
Revisions (0)
No revisions yet.