-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathedit-distance.py
More file actions
22 lines (20 loc) · 954 Bytes
/
edit-distance.py
File metadata and controls
22 lines (20 loc) · 954 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
class Solution:
def minDistance(self, word1: str, word2: str) -> int:
@lru_cache(maxsize = len(word1) * len(word2))
def backtrack(p1: int, p2: int) -> int:
# If either string is empty, then we need to do
# insertion operations to match the remaining characters of opposing word
if p1 == len(word1):
return len(word2) - p2
if p2 == len(word2):
return len(word1) - p1
# characters are equal, continue to next subproblem
if word1[p1] == word2[p2]:
return backtrack(p1 + 1, p2 + 1)
# characters not not equal, pick min cost operation
else:
delete = backtrack(p1 + 1, p2)
insert = backtrack(p1, p2 + 1)
replace = backtrack(p1 + 1, p2 + 1)
return 1 + min(delete, insert, replace)
return backtrack(0, 0)