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 + ... + snt = 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 <= 1000 <= s3.length <= 200s1,s2, ands3consist 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]
这篇博客探讨了一种中等难度的字符串问题——交错字符串。给出三个字符串s1, s2和s3,判断s3是否能由s1和s2的交错组合形成。作者提供了两种解决方案,一种是通过遍历s3并比较s1和s2的字符来实现,另一种是使用动态规划的方法。这两种方法的时间复杂度分别为O(n)和O(nm),空间复杂度分别为O(1)和O(nm)。博客包含了多个测试用例来验证解法的正确性。
1万+

被折叠的 条评论
为什么被折叠?



