给定三个字符串 s1、s2、s3,请你帮忙验证 s3 是否是由 s1 和 s2 交错组成的。
两个字符串 s 和 t 交错 的定义与过程如下,其中每个字符串都会被分割成若干非空子字符串:
- s = s1 + s2 + … + sn
- t = t1 + t2 + … + tm
- |n – m| <= 1
- 交错 是 s1 + t1 + s2 + t2 + s3 + t3 + … 或者 t1 + s1 + t2 + s2 + t3 + s3 + …
提示: a + b 意味着字符串 a 和 b 连接。
示例 1:
输入:s1 = "aabcc", s2 = "dbbca", s3 = "aadbbcbcac"
输出:true
示例 2:
输入:s1 = "aabcc", s2 = "dbbca", s3 = "aadbbbaccc"
输出:false
示例 3:
输入:s1 = "", s2 = "", s3 = ""
输出:true
提示:
- 0 <= s1.length, s2.length <= 100
- 0 <= s3.length <= 200
- s1、s2、和 s3 都由小写英文字母组成
Python 解答:
1.动态规划,可用滚动数组优化
class Solution:
def isInterleave(self, s1: str, s2: str, s3: str) -> bool:
m = len(s1)
n = len(s2)
if m+n != len(s3):
return False
if not m or not n:
return s1 == s3 or s2 == s3
dp = [[False for i in range(n+1)] for j in range(m+1)]
for i in range(1, m+1):
if s1[i-1] == s3[i-1]:
dp[i][0] = True
else:
break
for j in range(1, n+1):
if s2[j-1] == s3[j-1]:
dp[0][j] = True
else:
break
for j in range(1, n+1):
for i in range(1, m+1):
if s1[i-1] == s3[i+j-1]:
# dp[i][j] = (dp[i-1][j] or dp[i][j])
dp[i][j] = dp[i-1][j]
if s2[j-1] == s3[i+j-1]:
dp[i][j] = dp[i][j-1] or dp[i][j]
return dp[m][n]
留言