class Solution:
def isInterleave(self, s1: str, s2: str, s3: str) -> bool:
if (len(s3) != len(s1) + len(s2)): return False # 不加这个通过为6/101,里面由95个长度不行的???
dp = [[False] * (len(s2) + 1) for _ in range(len(s1) + 1)] # 长度注意为n+1
for i in range(len(s1) + 1):
for j in range(len(s2) + 1):
if i == j == 0:
dp[i][j] = True
elif i == 0:
dp[0][j] = dp[0][j - 1] and (s2[j - 1] == s3[j - 1]) # s2取[j - 1]非[j]
elif j == 0:
dp[i][0] = dp[i - 1][0] and (s1[i - 1] == s3[i - 1])
else:
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]))
print(dp)
return dp[len(s1)][len(s2)]
leetcode刷题python之交错字符串
最新推荐文章于 2023-10-25 11:43:45 发布