forked from anubhavshrimal/Data-Structures-Algorithms
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathEditDistance.py
More file actions
44 lines (32 loc) · 1.2 KB
/
EditDistance.py
File metadata and controls
44 lines (32 loc) · 1.2 KB
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
35
36
37
38
39
40
41
42
43
44
"""Given two strings str1 and str2 and below operations that can performed on str1. Find minimum number of edits (operations) required
to convert ‘str1’ into ‘str2’.
Insert
Remove
Replace
All of the above operations are of equal cost.
Example:
Input: str1 = "sunday", str2 = "saturday"
Output: 3
Last three and first characters are same. We basically
need to convert "un" to "atur". This can be done using
below three operations.
Replace 'n' with 'r', insert t, insert a"""
def edit_distance(str1, str2, m, n):
matrix = [[0 for i in range(n+1)] for i in range(m+1)]
for i in range(m+1):
for j in range(n+1):
if i == 0:
matrix[i][j] = j
elif j == 0:
matrix[i][j] = i
elif str1[i-1] == str2[j-1]:
matrix[i][j] = matrix[i-1][j-1]
else:
matrix[i][j] = 1 + min(matrix[i][j-1], # insert
matrix[i-1][j], # remove
matrix[i-1][j-1]) # replace
return matrix[m][n]
if __name__ == '__main__':
str1 = 'sunday'
str2 = 'saturday'
print(edit_distance(str1, str2, len(str1), len(str2)))