-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathInterLeave.py
More file actions
30 lines (26 loc) · 1005 Bytes
/
InterLeave.py
File metadata and controls
30 lines (26 loc) · 1005 Bytes
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
# 097. Interleaving String
# Dynamic Programming: dp[i][j]表示s1的前i位与s2的前j位能否匹配s3的前i+j位
# dp[i][j] = (dp[i-1][j] && s1[i-1] == s3[i-1+j]) || (dp[i][j-1] && s2[j-1] == s3[j-1+i])
class Solution:
def isInterleave(self, s1, s2, s3):
"""
:type s1: str
:type s2: str
:type s3: str
:rtype: bool
"""
if len(s1) + len(s2) != len(s3):
return False
m = len(s1) + 1
n = len(s2) + 1
dp = [[False] * n for i in range(m)]
dp[0][0] = True
for i in range(m):
for j in range(n):
if i == 0 and j >= 1:
dp[i][j] = dp[i][j-1] and s2[j-1] == s3[j-1]
elif j == 0 and i >= 1:
dp[i][j] = dp[i-1][j] and s1[i-1] == s3[i-1]
elif i >= 1 and j >= 1:
dp[i][j] = (dp[i][j-1] and s2[j-1] == s3[i+j-1]) or (dp[i-1][j] and s1[i-1] == s3[i+j-1])
return dp[m-1][n-1]