97. Interleaving String
Medium
2701133Add to ListShare
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 + ...
ort1 + 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
, ands3
consist of lowercase English letters.
Follow up: Could you solve it using only O(s2.length)
additional memory space?
错误解法
class Solution:
def isInterleave(self, s1: str, s2: str, s3: str) -> bool:
"""
# gg
assert Solution().isInterleave("aa", "ab", "aaba")
# AC
assert Solution().isInterleave("", "b", "b")
assert Solution().isInterleave("aa", "aa", "aaaa")
assert Solution().isInterleave("aabcc", "dbbca", "aadbbcbcac")
assert not Solution().isInterleave("aabcc", "dbbca", "aadbbbaccc")
assert Solution().isInterleave("", "", "")
解题思路:遍历s3,不断比较s1,s2当前索引位置的字符,若都相同则一直比较直到不同。
时间复杂度:O(n),空间复杂度:O(1)
"""
if len(s1) + len(s2) != len(s3):
return False
if len(s1) == 0:
return s2 == s3
if len(s2) == 0:
return s1 == s3
i = offset = index1 = index2 = 0
while index1 + offset < len(s1) and index2 + offset < len(s2):
if s1[index1 + offset] != s3[i] and s2[index2 + offset] != s3[i]:
return False
if s1[index1 + offset] == s3[i] and s2[index2 + offset] == s3[i]:
offset += 1
i += 1
continue
if s1[index1 + offset] == s3[i]:
index1 += (offset + 1)
if s2[index2 + offset] == s3[i]:
index2 += (offset + 1)
i += 1
offset = 0
# s3剩余字符串
str = s3[-(len(s3) - i)]
return str == s1[-(len(s1) - index1)] or str == s2[-(len(s2) - index2)]
AC
class Solution:
def isInterleave(self, s1: str, s2: str, s3: str) -> bool:
"""
assert not Solution().isInterleave("ab", "ccd", "acdab")
assert Solution().isInterleave("aa", "ab", "aaba")
assert Solution().isInterleave("", "b", "b")
assert Solution().isInterleave("aa", "aa", "aaaa")
assert Solution().isInterleave("aabcc", "dbbca", "aadbbcbcac")
assert not Solution().isInterleave("aabcc", "dbbca", "aadbbbaccc")
assert Solution().isInterleave("", "", "")
解题思路:看成一个二维地图,从左上角能不能走到右下角。
时间复杂度:O(nm),空间复杂度:O(nm)
如:("aa", "ab", "aaba")
X 0 a b
0 T T F
a T T T
a T F T
"""
n1, n2, n3 = len(s1), len(s2), len(s3)
if n1 + n2 != n3:
return False
if n1 == 0:
return s2 == s3
if n2 == 0:
return s1 == s3
dp = [[True for col in range(n2 + 1)] for row in range(n1 + 1)]
for i in range(1, n1 + 1):
dp[i][0] = dp[i - 1][0] and s1[i - 1] == s3[i - 1]
for i in range(1, n2 + 1):
dp[0][i] = dp[0][i - 1] and s2[i - 1] == s3[i - 1]
for i in range(1, n1 + 1):
for j in range(1, n2 + 1):
dp[i][j] = ((dp[i - 1][j] and s1[i - 1] == s3[i + j - 1]) or
(dp[i][j - 1] and s2[j - 1] == s3[i + j - 1]))
return dp[n1][n2]