-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy path0097.Interleaving_String.py
More file actions
61 lines (44 loc) · 1.77 KB
/
0097.Interleaving_String.py
File metadata and controls
61 lines (44 loc) · 1.77 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
60
61
Given strings s1, s2, and s3, find whether s3 is formed by an interleaving of s1 and s2.
An interleaving of two strings s and t is a configuration where they are divided into non-empty substrings such that:
s = s1 + s2 + ... + sn
t = t1 + t2 + ... + tm
|n - m| <= 1
The interleaving is s1 + t1 + s2 + t2 + s3 + t3 + ... or t1 + s1 + t2 + s2 + t3 + s3 + ...
Note: a + b is the concatenation of strings a and b.
Example 1:
Input: s1 = "aabcc", s2 = "dbbca", s3 = "aadbbcbcac"
Output: true
Example 2:
Input: s1 = "aabcc", s2 = "dbbca", s3 = "aadbbbaccc"
Output: false
Example 3:
Input: s1 = "", s2 = "", s3 = ""
Output: true
Constraints:
0 <= s1.length, s2.length <= 100
0 <= s3.length <= 200
s1, s2, and s3 consist of lowercase English letters.
"""
f[i][j]=s3的前[0..i+j)个字符能否由s1前i个字符[0..i)和s2前j个字符[0..j)交错形成
f[i][j]=True when (s3[i+j-1]=s1[i-1] 且 f[i-1][j]=True 即s3的前[0..i+j-1)个字符能否由s1前i-1个字符[0..i-1)和s2前j个字符[0..j)交错形成)
or (s3[i+j-1]=s2[j-1] and f[i][j-1]=True)
"""
class Solution:
def isInterleave(self, s1: str, s2: str, s3: str) -> bool:
m, n = len(s1), len(s2)
if len(s3) != m + n: return False
dp = [[False] * (n+1) for _ in range(m+1)]
dp[0][0] = True
for i in range(1, m+1):
if s1[i-1] == s3[i-1]:
dp[i][0] = dp[i-1][0]
for j in range(1, n+1):
if s2[j-1] == s3[j-1]:
dp[0][j] = dp[0][j-1]
for i in range(1, m+1):
for j in range(1, n+1):
if s1[i-1] == s3[i+j-1] and dp[i-1][j]:
dp[i][j] = True
if s2[j-1] == s3[i+j-1] and dp[i][j-1]:
dp[i][j] = True
return dp[m][n]