-
-
Notifications
You must be signed in to change notification settings - Fork 300
/
87.py
55 lines (46 loc) · 1.96 KB
/
87.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
__________________________________________________________________________________________________
sample 32 ms submission
import collections
class Solution:
def isScramble(self, s1: str, s2: str) -> bool:
if sorted(s1) != sorted(s2):
return False
def dfs(a,b):
n = len(a)
if n <= 2:
return True
front_cnt = collections.defaultdict(int)
back_cnt = collections.defaultdict(int)
for i in range(n - 1):
x, y, z = a[i], b[i], b[n - 1 - i]
front_cnt[x] += 1
if front_cnt[x] == 0: del front_cnt[x]
front_cnt[y] -= 1
if front_cnt[y] == 0: del front_cnt[y]
if len(front_cnt) == 0:
if dfs(a[:i + 1], b[:i + 1]) and dfs(a[i + 1:], b[i + 1:]):
return True
back_cnt[x] += 1
if back_cnt[x] == 0: del back_cnt[x]
back_cnt[z] -= 1
if back_cnt[z] == 0: del back_cnt[z]
if len(back_cnt) == 0:
if dfs(a[:i + 1], b[n - i - 1:]) and dfs(a[i + 1:], b[:n - i - 1]):
return True
return False
return dfs(s1, s2)
__________________________________________________________________________________________________
sample 13180 kb submission
class Solution:
def isScramble(self, s1, s2):
if( s1==s2):return True
if sorted(s1)!=sorted(s2):return False
for i in range(1,len(s1)):
a=self.isScramble(s1[:i],s2[:i])
b=self.isScramble(s1[i:],s2[i:])
if a and b:return True
a=self.isScramble(s1[i:],s2[:-i])
b=self.isScramble(s1[:i],s2[-i:])
if a and b:return True
return False
__________________________________________________________________________________________________