Added Levenshtein distance (a.k.a edit distance) algorithm to strings directory

This commit is contained in:
vaibhavrajsingh2001
2020-10-06 17:34:40 +05:30
parent e92b5d2a49
commit 2aa1426eba
2 changed files with 77 additions and 0 deletions

View File

@@ -0,0 +1,26 @@
import levenshteinDistance from './LevenshteinDistance'
describe('levenshteinDistance', () => {
it('should calculate edit distance between two strings', () => {
expect(levenshteinDistance('', '')).toBe(0)
expect(levenshteinDistance('a', '')).toBe(1)
expect(levenshteinDistance('', 'a')).toBe(1)
expect(levenshteinDistance('abc', '')).toBe(3)
expect(levenshteinDistance('', 'abc')).toBe(3)
// Should just add I to the beginning.
expect(levenshteinDistance('igloo', 'gloo')).toBe(1)
// Should just substitute i with o, m with g and insert e at end
expect(levenshteinDistance('firm', 'forge')).toBe(3)
// Should just substitute i with s, g with i, h with t and delete f from front
expect(levenshteinDistance('fighting', 'sitting')).toBe(4)
// Should add 4 letters b, a, s and e at the beginning.
expect(levenshteinDistance('ball', 'baseball')).toBe(4)
// Should delete 4 letters b, a, s and e at the beginning.
expect(levenshteinDistance('baseball', 'foot')).toBe(4)
})
})