classSolution:""" @params s1, s2, s3: Three strings as description. @return: return True if s3 is formed by the interleaving of s1 and s2 or False if not. @hint: you can use [[True] * m for i in range (n)] to allocate a n*m matrix. """defisInterleave(self,s1,s2,s3):# write your code hereifs1==Noneors2==Noneors3==None:returnFalsel1=len(s1)+1l2=len(s2)+1l3=len(s3)+1ifl3!=l1+l2-1:returnFalsedp=[[False]*l2foriinrange(l1)]# init dp if s1,s2,or s3 could be emptyforiinrange(l1-1):dp[i+1][0]=s1[:i+1]==s3[:i+1]foriinrange(l2-1):dp[0][i+1]=s2[:i+1]==s3[:i+1]dp[0][0]=Trueforiinrange(len(s1)):forjinrange(len(s2)):dp[i+1][j+1]=False# s3 choose s1ifs3[i+j+1]==s1[i]:dp[i+1][j+1]=dp[i][j+1]# s3 choose s2ifs3[i+j+1]==s2[j]:dp[i+1][j+1]|=dp[i+1][j]returndp[l1-1][l2-1]if__name__=="__main__":sol=Solution()printsol.isInterleave("aab","a","aaab")