Skip to content

Commit 075fe39

Browse files
committed
Add Levenshtein.
1 parent cb14892 commit 075fe39

File tree

2 files changed

+46
-1
lines changed

2 files changed

+46
-1
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@
3939
* [Least Common Multiple (LCM)](https://github.com/trekhleb/javascript-algorithms/tree/master/src/algorithms/math/least-common-multiple)
4040
* [Fisher–Yates Shuffle](https://github.com/trekhleb/javascript-algorithms/tree/master/src/algorithms/math/fisher-yates) - random permutation of a finite sequence
4141
* **String**
42-
* Minimum Edit distance (Levenshtein Distance)
42+
* [Levenshtein Distance](https://github.com/trekhleb/javascript-algorithms/tree/master/src/algorithms/string/levenshtein-distance) - minimum edit distance between two sequences
4343
* Hamming
4444
* Huffman
4545
* Knuth Morris Pratt
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
# Levenshtein Distance
2+
3+
The Levenshtein distance is a string metric for measuring the
4+
difference between two sequences. Informally, the Levenshtein
5+
distance between two words is the minimum number of
6+
single-character edits (insertions, deletions or substitutions)
7+
required to change one word into the other.
8+
9+
## Definition
10+
11+
Mathematically, the Levenshtein distance between two strings
12+
`a` and `b` (of length `|a|` and `|b|` respectively) is given by
13+
![Levenshtein](https://wikimedia.org/api/rest_v1/media/math/render/svg/4cf357d8f2135035207088d2c7b890fb4b64e410)
14+
where
15+
16+
![Levenshtein](https://wikimedia.org/api/rest_v1/media/math/render/svg/f0a48ecfc9852c042382fdc33c19e11a16948e85)
17+
18+
where
19+
![Levenshtein](https://wikimedia.org/api/rest_v1/media/math/render/svg/52512ede08444b13838c570ba4a3fc71d54dbce9)
20+
is the indicator function equal to `0` when
21+
![Levenshtein](https://wikimedia.org/api/rest_v1/media/math/render/svg/231fda9ee578f0328c5ca28088d01928bb0aaaec)
22+
and equal to 1 otherwise, and
23+
![Levenshtein](https://wikimedia.org/api/rest_v1/media/math/render/svg/bdc0315678caad28648aafedb6ebafb16bd1655c)
24+
is the distance between the first `i` characters of `a` and the first
25+
`j` characters of `b`.
26+
27+
Note that the first element in the minimum corresponds to
28+
deletion (from `a` to `b`), the second to insertion and
29+
the third to match or mismatch, depending on whether the
30+
respective symbols are the same.
31+
32+
## Example
33+
34+
For example, the Levenshtein distance between `kitten` and
35+
`sitting` is `3`, since the following three edits change one
36+
into the other, and there is no way to do it with fewer than
37+
three edits:
38+
39+
1. **k**itten → **s**itten (substitution of "s" for "k")
40+
2. sitt**e**n → sitt**i**n (substitution of "i" for "e")
41+
3. sittin → sittin**g** (insertion of "g" at the end).
42+
43+
## References
44+
45+
- [Wikipedia](https://en.wikipedia.org/wiki/Levenshtein_distance)

0 commit comments

Comments
 (0)