-
Notifications
You must be signed in to change notification settings - Fork 481
/
0072.py
31 lines (30 loc) · 956 Bytes
/
0072.py
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
from heapq import heappush, heappop
class Solution:
def minDistance(self, word1, word2):
"""
:type word1: str
:type word2: str
:rtype: int
"""
heap = [(0, word1, word2)]
visited = set()
while heap:
d, w1, w2 = heappop(heap)
if (w1, w2) in visited:
continue
visited.add((w1, w2))
if w1 == w2:
return d
if w1 and w2 and w1[0] == w2[0]:
heappush(heap, (d, w1[1:], w2[1:]))
else:
if w1:
heappush(heap, (d+1, w1[1:], w2)) #delete
if w1 and w2:
heappush(heap, (d+1, w1[1:], w2[1:])) #replace
if w2:
heappush(heap, (d+1, w1, w2[1:])) #add
if __name__ == "__main__":
word1 = "horse"
word2 = "ros"
print(Solution().minDistance(word1, word2))