-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathlevenshtein.py
More file actions
34 lines (26 loc) · 979 Bytes
/
levenshtein.py
File metadata and controls
34 lines (26 loc) · 979 Bytes
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
def levenshtein(word1: str, word2: str) -> int:
matrix: list[list[int]] = [[] for _ in range(len(word1) + 1)]
for num in range(len(word2) + 1):
matrix[0].append(num)
for num in range(1, len(word1) + 1):
matrix[num].append(num)
for i in range(1, len(word1) + 1):
for j in range(1, len(word2) + 1):
if word1[i - 1] != word2[j - 1]:
levens = (
min(
(
matrix[i - 1][j], # top
matrix[i][j - 1], # left
matrix[i - 1][j - 1], # digonal
)
)
+ 1
)
else:
levens = matrix[i - 1][j - 1]
matrix[i].append(levens)
return matrix[len(word1)][len(word2)]
if __name__ == "__main__":
print(levenshtein("kitten", "sitting"))
print(levenshtein("flaw", "lawn"))