-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path97.py
More file actions
31 lines (26 loc) · 1 KB
/
97.py
File metadata and controls
31 lines (26 loc) · 1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# -*- coding:utf-8 -*-
# s3中只会有s1和s2,写一个二维数组就可以了
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
res_list = [[False for i in range(len(s2)+1)] for j in range(len(s1)+1)]
for i in range(len(s1)+1):
for j in range(len(s2)+1):
if i==0 and j==0:
res_list[i][j] = True
elif i==0:
res_list[i][j] = res_list[i][j-1] & (s2[j-1] == s3[j-1])
elif j == 0:
res_list[i][j] = res_list[i-1][j] & (s1[i-1] == s3[i-1])
else:
res_list[i][j] = (res_list[i][j-1]&(s2[j-1]==s3[i+j-1])) or (res_list[i-1][j]&(s1[i-1]==s3[i+j-1]))
return res_list[len(s1)][len(s2)]
solu = Solution()
print solu.isInterleave('aabcc', 'dbbca', 'aadbbcbcac')