FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
leetcode-algorithms/python/scramble_strings.py at master · anishLearnsToCode/leetcode-algorithms · GitHub
anishLearnsToCode
/
leetcode-algorithms
Public
Notifications
You must be signed in to change notification settings
Fork
17
Star
98
Code
Issues
0
Pull requests
0
Discussions
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Discussions
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
leetcode-algorithms
/
python
/
scramble_strings.py
Copy path
More file actions
More file actions
Latest commit
History
History
History
46 lines (41 loc) · 1.51 KB
Breadcrumbs
leetcode-algorithms
/
python
/
scramble_strings.py
Copy path
File metadata and controls
46 lines (41 loc) · 1.51 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
# https://leetcode.com/problems/scramble-string/description/
# T: O(n^4) where n is the number of characters in the string
# S: O(n^3) where n is the number of characters in the string
from
collections
import
defaultdict
class
Solution
:
def
isScramble
(
self
,
s1
:
str
,
s2
:
str
)
->
bool
:
cache
=
dict
()
return
self
.
helper
(
s1
,
s2
,
cache
)
def
helper
(
self
,
s1
:
str
,
s2
:
str
,
cache
:
dict
)
->
bool
:
key
=
(
s1
,
s2
)
key_r
=
(
s2
,
s1
)
if
key
in
cache
:
return
cache
[
key
]
if
key_r
in
cache
:
return
cache
[
key_r
]
# If not cached
n
=
len
(
s1
)
# Base case
if
sorted
(
s1
)
!=
sorted
(
s2
):
cache
[
key
]
=
False
return
False
if
n
<=
3
:
cache
[
key
]
=
True
return
True
# split sting for comparision
count_s1
=
defaultdict
(
int
)
count_s2
=
defaultdict
(
int
)
count_s2_r
=
defaultdict
(
int
)
for
i
in
range
(
1
,
n
):
count_s1
[
s1
[
i
-
1
]]
+=
1
count_s2
[
s2
[
i
-
1
]]
+=
1
count_s2_r
[
s2
[
-
i
]]
+=
1
if
count_s1
==
count_s2
:
cache
[
key
]
=
self
.
helper
(
s1
[
0
:
i
],
s2
[
0
:
i
],
cache
)
and
self
.
helper
(
s1
[
i
:
n
],
s2
[
i
:
n
],
cache
)
if
cache
[
key
]:
return
True
if
count_s1
==
count_s2_r
:
cache
[
key
]
=
self
.
helper
(
s1
[
0
:
i
],
s2
[
n
-
i
:
n
],
cache
)
and
self
.
helper
(
s1
[
i
:
n
],
s2
[
0
:
n
-
i
],
cache
)
if
cache
[
key
]:
return
True
return
False
Back
|
FazBrowse Home
|
New Git URL