class Solution:
"""
@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.
"""
def isInterleave(self, s1, s2, s3):
# write your code here
m , n = len(s1), len(s2)
if m + n != len(s3):
return False
f = [[False] * (n + 1) for x in range(m + 1)]
f[0][0] = True
for i in range(1, m + 1):
if s1[i - 1] == s3[i - 1] and f[i - 1][0]:
f[i][0] = True
for i in range(1, n + 1):
if s2[i - 1] == s3[i - 1] and f[0][i - 1]:
f[0][i] = True
for i in range(1, m + 1):
for j in range(1, n + 1):
if (s1[i - 1] == s3[i + j - 1] and f[i - 1][j]) or (s2[j - 1] == s3 [i + j - 1] and f[i][j - 1]):
f[i][j] = True
return f[m][n]