forked from Garvit244/Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
97.py
41 lines (33 loc) · 1.09 KB
/
97.py
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
32
33
34
35
36
37
38
39
40
41
'''
Given s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2.
Example 1:
Input: s1 = "aabcc", s2 = "dbbca", s3 = "aadbbcbcac"
Output: true
Example 2:
Input: s1 = "aabcc", s2 = "dbbca", s3 = "aadbbbaccc"
Output: false
'''
class Solution(object):
def isInterleave(self, s1, s2, s3):
"""
:type s1: str
:type s2: str
:type s3: str
:rtype: bool
"""
if len(s3) != len(s1) + len(s2):
return False
dp = [[False for _ in range(len(s2)+1)] for _ in range(len(s1)+1)]
for row in range(len(s1)+1):
for col in range(len(s2)+1):
if row == 0 and col == 0:
dp[row][col] = True
elif row == 0:
dp[row][col] =dp[row][col-1] and s2[col-1] == s3[row+col-1]
elif col == 0:
dp[row][col] = dp[row-1][col] and s1[row-1] == s3[row+col-1]
else:
dp[row][col] = (dp[row][col-1] and s2[col-1] == s3[row+col-1]) or (dp[row-1][col] and s1[row-1] == s3[row+col-1])
return dp[len(s1)][len(s2)]
# Time: O(m*n)
# Space: O(m*n)