Skip to content

Commit 84eedf2

Browse files
authored
Create 87-scramble-string.js
1 parent cd579cf commit 84eedf2

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed

87-scramble-string.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/**
2+
* @param {string} s1
3+
* @param {string} s2
4+
* @return {boolean}
5+
*/
6+
const isScramble = function(s1, s2) {
7+
if (s1 === s2) return true
8+
const letters = new Array(128).fill(0)
9+
const a = 'a'.charCodeAt(0)
10+
for (let i = 0; i < s1.length; i++) {
11+
letters[s1.charCodeAt(i) - a]++
12+
letters[s2.charCodeAt(i) - a]--
13+
}
14+
for (let i = 0; i < 128; i++) if (letters[i] !== 0) return false
15+
for (let i = 1; i < s1.length; i++) {
16+
if (
17+
isScramble(s1.substring(0, i), s2.substring(0, i)) &&
18+
isScramble(s1.substring(i), s2.substring(i))
19+
)
20+
return true
21+
if (
22+
isScramble(s1.substring(0, i), s2.substring(s2.length - i)) &&
23+
isScramble(s1.substring(i), s2.substring(0, s2.length - i))
24+
)
25+
return true
26+
}
27+
return false
28+
}

0 commit comments

Comments
 (0)