Skip to content

Commit a77de02

Browse files
hinevicsabranhe
authored andcommitted
add algorithm for calculating Levenshtein Distance
1 parent aef86a8 commit a77de02

File tree

1 file changed

+9
-0
lines changed

1 file changed

+9
-0
lines changed

algorithms/strings/levenstein.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
def distance_levenstein(a, b):
2+
f = [[(i+j) if i*j == 0 else 0 for j in range(len(b)+1)] for i in range(len(a) + 1)]
3+
for i in range(1, len(a) + 1):
4+
for j in range(1, len(b) + 1):
5+
if a[i-1] == b[j-1]:
6+
f[i][j] = f[i-1][j-1]
7+
else:
8+
f[i][j] = 1 + min(f[i-1][j], f[i][j-1], f[i-1][j-1])
9+
return f[len(a)][len(b)]

0 commit comments

Comments
 (0)