class Solution:
def isInterleave(self, s1: str, s2: str, s3: str) -> bool:
# 使用dp矩阵
len1 = len(s1)
len2 = len(s2)
len3 = len(s3)
# 如果s3的长度和s1+s2的不等,就不行
if len1 + len2 != len3:
return False
# dp数组全是False
dp = [[False]*(len2+1) for _ in range(len1+1)]
# 对于两个数组都是空的情况
dp[0][0] = True
# 对于第一行,则判断s2是否能构成s3
for j in range(1,len2+1):
if dp[0][j-1] == False:
break
else:
dp[0][j] = (dp[0][j-1] and s2[j-1]==s3[j-1])
# 对于第一列,则判断s1是否能构成s3
for i in range(1,len1+1):
if dp[i-1][0] == False:
break
else:
dp[i][0] = (dp[i-1][0] and s1[i-1]==s3[i-1])
# 对于其他位置的情况,依次遍历,从左到右,从上到下
for i in range(1,len1+1):
for j in range(1,len2+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[-1][-1]