-
-
Notifications
You must be signed in to change notification settings - Fork 38
/
0097-interleaving-string.py
45 lines (39 loc) · 1.47 KB
/
0097-interleaving-string.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
42
43
44
45
# time complexity: O(mn)
# space complexity: O(mn)
from functools import lru_cache
import numpy as np
class Solution:
def isInterleave(self, s1: str, s2: str, s3: str) -> bool:
if len(s3) != len(s1) + len(s2):
return False
dp = np.full([len(s1) + 1, len(s2) + 1], False)
dp[0][0] = True
for i in range(len(s1) + 1):
for j in range(len(s2) + 1):
if i > 0 and s1[i-1] == s3[i + j - 1]:
dp[i][j] |= dp[i - 1, j]
if j > 0 and s2[j-1] == s3[i + j - 1]:
dp[i][j] |= dp[i, j - 1]
return dp[len(s1), len(s2)]
# class Solution:
# def isInterleave(self, s1: str, s2: str, s3: str) -> bool:
# m, n, o = len(s1), len(s2), len(s3)
# if m+n != o:
# return False
# dp = [[False] * (n+1) for _ in range(m+1)]
# for i in range(m+1):
# for j in range(n+1):
# if i == 0 and j == 0:
# dp[i][j] = True
# elif i == 0:
# dp[i][j] = dp[i][j-1] and s2[j-1] == s3[i+j-1]
# elif j == 0:
# dp[i][j] = dp[i-1][j] and s1[i-1] == s3[i+j-1]
# else:
# dp[i][j] = (dp[i-1][j] and s1[i-1] == s3[i+j-1]
# ) or (dp[i][j-1] and s2[j-1] == s3[i+j-1])
# return dp[m][n]
s1 = "aabcc"
s2 = "dbbca"
s3 = "aadbbcbcac"
print(Solution().isInterleave(s1, s2, s3))