Skip to content

Commit beee32b

Browse files
authored
Merge pull request #427 from vaibhavrajsingh2001/master
Added Levenshtein distance algorithm
2 parents 15f1be6 + 2aa1426 commit beee32b

File tree

2 files changed

+77
-0
lines changed

2 files changed

+77
-0
lines changed

String/LevenshteinDistance.js

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/* The Levenshtein distance (a.k.a edit distance) is a
2+
measure of similarity between two strings. It is
3+
defined as the minimum number of changes required to
4+
convert string a into string b (this is done by
5+
inserting, deleting or replacing a character in
6+
string a).
7+
The smaller the Levenshtein distance,
8+
the more similar the strings are. This is a very
9+
common problem in the application of Dynamic Programming.
10+
*/
11+
12+
const levenshteinDistance = (a, b) => {
13+
// Declaring array 'D' with rows = len(a) + 1 and columns = len(b) + 1:
14+
const distanceMatrix = Array(b.length + 1)
15+
.fill(null)
16+
.map(() => Array(a.length + 1).fill(null))
17+
18+
// Initialising first column:
19+
for (let i = 0; i <= a.length; i += 1) {
20+
distanceMatrix[0][i] = i
21+
}
22+
23+
// Initialising first row:
24+
for (let j = 0; j <= b.length; j += 1) {
25+
distanceMatrix[j][0] = j
26+
}
27+
28+
for (let j = 1; j <= b.length; j += 1) {
29+
for (let i = 1; i <= a.length; i += 1) {
30+
const indicator = a[i - 1] === b[j - 1] ? 0 : 1
31+
// choosing the minimum of all three, vis-a-vis:
32+
distanceMatrix[j][i] = Math.min(
33+
distanceMatrix[j][i - 1] + 1, // deletion
34+
distanceMatrix[j - 1][i] + 1, // insertion
35+
distanceMatrix[j - 1][i - 1] + indicator // substitution
36+
)
37+
}
38+
}
39+
40+
console.log(
41+
'Levenshtein Distance between ' +
42+
a +
43+
' and ' +
44+
b +
45+
' is = ' +
46+
distanceMatrix[b.length][a.length]
47+
)
48+
return distanceMatrix[b.length][a.length]
49+
}
50+
51+
export { levenshteinDistance }

String/LevenshteinDistance.test.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import levenshteinDistance from './LevenshteinDistance'
2+
3+
describe('levenshteinDistance', () => {
4+
it('should calculate edit distance between two strings', () => {
5+
expect(levenshteinDistance('', '')).toBe(0)
6+
expect(levenshteinDistance('a', '')).toBe(1)
7+
expect(levenshteinDistance('', 'a')).toBe(1)
8+
expect(levenshteinDistance('abc', '')).toBe(3)
9+
expect(levenshteinDistance('', 'abc')).toBe(3)
10+
11+
// Should just add I to the beginning.
12+
expect(levenshteinDistance('igloo', 'gloo')).toBe(1)
13+
14+
// Should just substitute i with o, m with g and insert e at end
15+
expect(levenshteinDistance('firm', 'forge')).toBe(3)
16+
17+
// Should just substitute i with s, g with i, h with t and delete f from front
18+
expect(levenshteinDistance('fighting', 'sitting')).toBe(4)
19+
20+
// Should add 4 letters b, a, s and e at the beginning.
21+
expect(levenshteinDistance('ball', 'baseball')).toBe(4)
22+
23+
// Should delete 4 letters b, a, s and e at the beginning.
24+
expect(levenshteinDistance('baseball', 'foot')).toBe(4)
25+
})
26+
})

0 commit comments

Comments
 (0)