Skip to content

Commit

Permalink
Recursive case
Browse files Browse the repository at this point in the history
  • Loading branch information
jendapang committed Apr 13, 2025
1 parent 7269948 commit 253a95b
Showing 1 changed file with 11 additions and 1 deletion.
12 changes: 11 additions & 1 deletion Levenshtein_Edit_Distance.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,14 @@ def edit_distance(str1, str2): #input is 2 strings that we will be comparing
for j in range(len(str2)+1):
D[0][j] = j

# Fill in rest of matrix with recursive case
#fill in rest of matrix with recursive case
for i in range(1, len(str1)+1):
for j in range(1, len(str2)+1):
if str1[i-1] == str2[j-1]:
cost = 0 #match: no operation needed
else:
cost = 1 #no match: substitution
D[i][j] = min(D[i-1][j] + 1, D[i][j-1] + 1,D[i-1][j-1] + cost)

return D[len(str1)][len(str2)] #last cell of matrix contains optimal solution

0 comments on commit 253a95b

Please sign in to comment.