2
* The recursive solution with memoization.3
* This solution doesn't use `string.slice()`, but pass substring boundaries directly to function calls.5
* Time Complexity: O(n^2)6
* Space Complexity: O(n^2)12
var isScramble = function (s1, s2) {13
return checkScramble(s1, 0, s1.length, s2, 0, s2.length);16
function checkScramble(string1, i1, j1, string2, i2, j2, memory = {}) {18
const key = 1e9 * i1 + 1e6 * j1 + 1e3 * i2 + 1 * j2;26
for (let i = 0; i < n; i++) {28
string1.charCodeAt(i1 + i) ** 2 - string2.charCodeAt(i2 + i) ** 2;32
return (memory[key] = false);36
return (memory[key] = true);39
for (let i = 1; i < n; i++) {41
checkScramble(string1, i1, i1 + i, string2, i2, i2 + i, memory) &&42
checkScramble(string1, i1 + i, j1, string2, i2 + i, j2, memory)44
return (memory[key] = true);48
checkScramble(string1, i1, i1 + i, string2, j2 - i, j2, memory) &&49
checkScramble(string1, i1 + i, j1, string2, i2, j2 - i, memory)51
return (memory[key] = true);55
return (memory[key] = false);