-
Notifications
You must be signed in to change notification settings - Fork 1.5k
/
Copy path072_Edit_Distance.py
45 lines (42 loc) · 1.54 KB
/
072_Edit_Distance.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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
class Solution(object):
# https://discuss.leetcode.com/topic/17639/20ms-detailed-explained-c-solutions-o-n-space/2
# def minDistance(self, word1, word2):
# """
# :type word1: str
# :type word2: str
# :rtype: int
# """
# ls_1, ls_2 = len(word1), len(word2)
# dp = [[0] * (ls_2 + 1) for _ in range(ls_1 + 1)]
# for i in range(1, ls_1 + 1):
# dp[i][0] = i
# for j in range(1, ls_2 + 1):
# dp[0][j] = j
# for i in range(1, ls_1 + 1):
# for j in range(1, ls_2 + 1):
# if word1[i - 1] == word2[j - 1]:
# dp[i][j] = dp[i - 1][j - 1]
# else:
# dp[i][j] = min(dp[i - 1][j - 1] + 1,
# dp[i][j - 1] + 1,
# dp[i - 1][j] + 1)
# return dp[ls_1][ls_2]
def minDistance(self, word1, word2):
ls_1, ls_2 = len(word1), len(word2)
dp = list(range(ls_1 + 1))
for j in range(1, ls_2 + 1):
pre = dp[0]
dp[0] = j
for i in range(1, ls_1 + 1):
temp = dp[i]
if word1[i - 1] == word2[j - 1]:
dp[i] = pre
else:
dp[i] = min(pre + 1, dp[i] + 1, dp[i - 1] + 1)
pre = temp
return dp[ls_1]
if __name__ == '__main__':
# begin
s = Solution()
print (s.minDistance("horse","ros"))
print (s.minDistance("intention","execution"))