Skip to content

Commit b77ae38

Browse files
Dwipam KatariyaDwipam Katariya
authored andcommitted
Add edit distance
1 parent 88c9cc0 commit b77ae38

1 file changed

Lines changed: 19 additions & 0 deletions

File tree

practice/edit_distance.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
str1 = "Sunday"
2+
str2 = "Monday"
3+
4+
def edit_distance(str1, str2):
5+
m = len(str1)
6+
n = len(str2)
7+
dist = [[0 for i in range(m+1)] for i in range(n+1)]
8+
for i in range(n+1):
9+
for j in range(m+1):
10+
if i==0:
11+
dist[i][j] == j
12+
if j==0:
13+
dist[i][j] == i
14+
elif str1[i-1] == str2[j-1]:
15+
dist[i][j] = dist[i-1][j-1]
16+
else:
17+
dist[i][j] = 1 + min(dist[i-1][j],dist[j-1][i],dist[i-1][j-1])
18+
return dist[n][m]
19+
print edit_distance(str1, str2)

0 commit comments

Comments
 (0)