您的位置:首页 > 其它

97. Interleaving String

2017-06-29 14:56 281 查看
Given s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2.

For example,

Given:
s1 = 
"aabcc"
,
s2 = 
"dbbca"
,

When s3 = 
"aadbbcbcac"
, return true.

When s3 = 
"aadbbbaccc"
, return false.

class Solution(object):
def isInterleave(self, s1, s2, s3):
"""
:type s1: str
:type s2: str
:type s3: str
:rtype: bool
"""
if len(s1) + len(s2) != len(s3):
return False
m = [[False for _ in xrange(len(s1) + 1)] for _ in xrange(len(s2) + 1)]
m[0][0] = True
for i in xrange(1, len(s1)+1):
m[0][i] = m[0][i - 1] and (s1[i - 1] == s3[i - 1])
for i in xrange(1, len(s2)+1):
m[i][0] = m[i - 1][0] and (s2[i - 1] == s3[i - 1])
for i in xrange(1, len(s2)+1):
for j in xrange(1, len(s1)+1):
m[i][j] = (m[i][j - 1] and s1[j - 1] == s3[i+j-1]) or (m[i-1][j] and s2[i - 1] == s3[i+j-1])
return m[len(s2)][len(s1)]
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: