-
Notifications
You must be signed in to change notification settings - Fork 35
Expand file tree
/
Copy path29_interleaving_string.py
More file actions
59 lines (47 loc) · 1.55 KB
/
29_interleaving_string.py
File metadata and controls
59 lines (47 loc) · 1.55 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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
class Solution:
"""
@param: A: A string
@param: B: A string
@param: C: A string
@return: Determine whether C is formed by interleaving of A and B
"""
def isInterleave(self, A, B, C):
if A is None or B is None or C is None:
return False
if A is '' and B is '' and C is '':
return True
a, b, c = len(A), len(B), len(C)
if c != a + b:
return False
"""
`dp[i][j]` means the substr end at `C[i + j - 1]`
is mixed by the substr end at `A[i - 1]`
and the substr end at `B[j - 1]`
"""
dp = [[False] * (b + 1) for _ in range(2)]
prev = curr = 0
dp[curr][0] = True
for j in range(1, b + 1):
"""
dp[0][j] = (dp[0][j - 1] and B[j - 1] == C[j - 1])
"""
if not dp[curr][j - 1]:
break
if B[j - 1] == C[j - 1]:
dp[curr][j] = True
for i in range(1, a + 1):
prev = curr
curr = 1 - curr
"""
dp[i][0] = (dp[i - 1][0] and A[i - 1] == C[i - 1])
"""
if dp[prev][0] and A[i - 1] == C[i - 1]:
dp[curr][0] = True
for j in range(1, b + 1):
dp[curr][j] = False
if dp[prev][j] and A[i - 1] == C[i + j - 1]:
dp[curr][j] = True
continue
if dp[curr][j - 1] and B[j - 1] == C[i + j - 1]:
dp[curr][j] = True
return dp[curr][b]