forked from SamirPaulb/LeetCode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path087_Scramble_String.py
81 lines (77 loc) · 2.96 KB
/
087_Scramble_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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
class Solution(object):
#https://discuss.leetcode.com/topic/20094/my-c-solutions-recursion-with-cache-dp-recursion-with-cache-and-pruning-with-explanation-4ms/2
# def isScramble(self, s1, s2):
# """
# :type s1: str
# :type s2: str
# :rtype: bool
# """
# # recursive
# if s1 == s2:
# return True
# if len(s1) != len(s2):
# return False
# ls = len(s1)
# letters = [0] * 26
# for i in range(ls):
# letters[ord(s1[i]) - ord('a')] += 1
# letters[ord(s2[i]) - ord('a')] -= 1
# for i in range(26):
# if letters[i] != 0:
# return False
# for i in range(1, ls):
# if self.isScramble(s1[0:i], s2[0:i]) and self.isScramble(s1[i:], s2[i:]):
# return True
# if self.isScramble(s1[0:i], s2[ls - i:]) and self.isScramble(s1[i:], s2[:ls - i]):
# return True
# return False
def isScramble(self, s1, s2, memo={}):
# recursive with memo
# Check with sorted is fundamental, otherwise TLE
if len(s1) != len(s2) or sorted(s1) != sorted(s2):
return False
if len(s1) <= len(s2) <= 1:
return s1 == s2
if s1 == s2:
return True
if (s1, s2) in memo:
return memo[s1, s2]
n = len(s1)
for i in range(1, n):
a = self.isScramble(s1[:i], s2[:i], memo) and self.isScramble(s1[i:], s2[i:], memo)
if not a:
b = self.isScramble(s1[:i], s2[-i:], memo) and self.isScramble(s1[i:], s2[:-i], memo)
if a or b:
memo[s1, s2] = True
return True
memo[s1, s2] = False
return False
# def isScramble(self, s1, s2):
# # dp TLE
# if s1 == s2:
# return True
# if len(s1) != len(s2):
# return False
# ls = len(s1)
# letters = [0] * 26
# for i in range(ls):
# letters[ord(s1[i]) - ord('a')] += 1
# letters[ord(s2[i]) - ord('a')] -= 1
# for i in range(26):
# if letters[i] != 0:
# return False
# dp = [[[False] * ls for i in range(ls)] for i in range(ls + 1)]
# for i in range(ls):
# for j in range(ls):
# dp[1][i][j] = (s1[i] == s2[j])
#
# for cur_len in range(2, ls + 1):
# for i in range(ls - cur_len + 1):
# for j in range(ls - cur_len + 1):
# dp[cur_len][i][j] = False
# for k in range(1, cur_len):
# if dp[cur_len][i][j]:
# break
# dp[cur_len][i][j] = dp[cur_len][i][j] or (dp[k][i][j] and dp[cur_len - k][i + k][j + k])
# dp[cur_len][i][j] = dp[cur_len][i][j] or (dp[k][i + cur_len - k][j] and dp[cur_len - k][i][j + k])
# return dp[ls][0][0]