-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path583 Delete Operation for Two Strings.py
78 lines (63 loc) · 2.24 KB
/
583 Delete Operation for Two Strings.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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#!/usr/bin/python3
"""
Given two words word1 and word2, find the minimum number of steps required to
make word1 and word2 the same, where in each step you can delete one character
in either string.
Example 1:
Input: "sea", "eat"
Output: 2
Explanation: You need one step to make "sea" to "ea" and another step to make "eat" to "ea".
Note:
The length of given words won't exceed 500.
Characters in given words can only be lower-case letters.
"""
from collections import defaultdict
class Solution:
def minDistance(self, word1: str, word2: str) -> int:
"""
Longest Common Subsequence (LCS)
Find the LCS, and delete the char in BOTH strings into LCS
Let F[i][j] be length of LCS word1[:i] and word2[:j]
F[i][j] = F[i-1][j-1] + 1 if word1[i-1] == word2[j-1]
F[i][j] = max(F[i-1][j], F[i][j-1])
"""
F = defaultdict(lambda: defaultdict(int))
m = len(word1)
n = len(word2)
for i in range(1, m + 1):
for j in range(1, n + 1):
if word1[i-1] == word2[j-1]:
F[i][j] = F[i-1][j-1] + 1
else:
F[i][j] = max(
F[i-1][j],
F[i][j-1],
)
return m - F[m][n] + n - F[m][n]
def minDistance_edit_distance(self, word1: str, word2: str) -> int:
"""
Edit distance
Let F[i][j] be # operations to make same for word1[:i] and word2[:j]
F[i][j] = F[i-1][j-1] if word1[i-1] == word2[j-1]
F[i][j] = min(F[i-1][j] + 1, F[i][j-1] + 1)
"""
F = defaultdict(lambda: defaultdict(int))
m = len(word1)
n = len(word2)
# initialization is important
for i in range(1, m + 1):
F[i][0] = i
for j in range(1, n + 1):
F[0][j] = j
for i in range(1, m + 1):
for j in range(1, n + 1):
if word1[i-1] == word2[j-1]:
F[i][j] = F[i-1][j-1]
else:
F[i][j] = min(
F[i-1][j] + 1,
F[i][j-1] + 1,
)
return F[m][n]
if __name__ == "__main__":
assert Solution().minDistance("sea", "eat") == 2