FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
JavaScript/String/ScrambleStrings.js at master · TheAlgorithms/JavaScript · GitHub
Uh oh!
There was an error while loading.
Please reload this page
.
TheAlgorithms
/
JavaScript
Public
Uh oh!
There was an error while loading.
Please reload this page
.
Notifications
You must be signed in to change notification settings
Fork
5.8k
Star
34.2k
Code
Issues
21
Pull requests
192
Actions
Projects
Wiki
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Projects
Wiki
Security and quality
Insights
Expand file tree
Breadcrumbs
JavaScript
/
String
/
ScrambleStrings.js
Copy path
More file actions
More file actions
Latest commit
History
History
History
50 lines (41 loc) · 1.15 KB
Breadcrumbs
JavaScript
/
String
/
ScrambleStrings.js
Copy path
File metadata and controls
50 lines (41 loc) · 1.15 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
47
48
49
50
// Problem Statement and Explanation: https://leetcode.com/problems/scramble-string/
/**
* Given two strings s1 and s2 of the same length, return true if s2 is a scrambled string of s1, otherwise, return false.
*
@param
{
string
} s1
*
@param
{
string
} s2
*
@return
{
boolean
}
*/
const
isScramble
=
(
s1
,
s2
)
=>
{
return
helper
(
{
}
,
s1
,
s2
)
}
const
helper
=
function
(
dp
,
s1
,
s2
)
{
const
map
=
{
}
if
(
dp
[
s1
+
s2
]
!==
undefined
)
return
dp
[
s1
+
s2
]
if
(
s1
===
s2
)
return
true
for
(
let
j
=
0
;
j
<
s1
.
length
;
j
++
)
{
if
(
map
[
s1
[
j
]
]
===
undefined
)
map
[
s1
[
j
]
]
=
0
if
(
map
[
s2
[
j
]
]
===
undefined
)
map
[
s2
[
j
]
]
=
0
map
[
s1
[
j
]
]
++
map
[
s2
[
j
]
]
--
}
for
(
const
key
in
map
)
{
if
(
map
[
key
]
!==
0
)
{
dp
[
s1
+
s2
]
=
false
return
false
}
}
for
(
let
i
=
1
;
i
<
s1
.
length
;
i
++
)
{
if
(
(
helper
(
dp
,
s1
.
substr
(
0
,
i
)
,
s2
.
substr
(
0
,
i
)
)
&&
helper
(
dp
,
s1
.
substr
(
i
)
,
s2
.
substr
(
i
)
)
)
||
(
helper
(
dp
,
s1
.
substr
(
0
,
i
)
,
s2
.
substr
(
s2
.
length
-
i
)
)
&&
helper
(
dp
,
s1
.
substr
(
i
)
,
s2
.
substr
(
0
,
s2
.
length
-
i
)
)
)
)
{
dp
[
s1
+
s2
]
=
true
return
true
}
}
dp
[
s1
+
s2
]
=
false
return
false
}
export
{
isScramble
}
Back
|
FazBrowse Home
|
New Git URL