-
-
Notifications
You must be signed in to change notification settings - Fork 48
Expand file tree
/
Copy path0087-scramble-string.py
More file actions
31 lines (29 loc) · 1.02 KB
/
0087-scramble-string.py
File metadata and controls
31 lines (29 loc) · 1.02 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
# time complexity: O(n^4)
# space complexity: O(n^3)
class Solution:
def isScramble(self, s1: str, s2: str) -> bool:
n = len(s1)
dp = [[[False for _ in range(n)] for _ in range(n)]
for _ in range(n + 1)]
for i in range(n):
for j in range(n):
dp[1][i][j] = s1[i] == s2[j]
for length in range(2, n + 1):
for i in range(n + 1 - length):
for j in range(n + 1 - length):
for newLength in range(1, length):
dp1 = dp[newLength][i]
dp2 = dp[length - newLength][i + newLength]
dp[length][i][j] |= dp1[j] and dp2[j + newLength]
dp[length][i][j] |= (
dp1[j + length - newLength] and dp2[j])
return dp[n][0][0]
s1 = "great"
s2 = "rgeat"
print(Solution().isScramble(s1, s2))
s1 = "abcde"
s2 = "caebd"
print(Solution().isScramble(s1, s2))
s1 = "a"
s2 = "a"
print(Solution().isScramble(s1, s2))