-
Notifications
You must be signed in to change notification settings - Fork 4
/
scramble-string.js
51 lines (48 loc) · 1.25 KB
/
scramble-string.js
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
var DEBUG = process.env.DEBUG;
/**
* @param {string} s1
* @param {string} s2
* @return {boolean}
*/
var isScramble = function(s1, s2) {
var m = {};
function set(s, t, v) {
m[s] = m[s] || {};
m[s][t] = v;
return v;
}
function get(s, t) {
if (m[s] === undefined) return m[s];
else return m[s][t];
}
function f(s, t) {
var v;
if ((v = get(s, t)) !== undefined) return v;
if (s === t) return set(s, t, true);
for (var i = 1; i < s.length; i++) {
if (f(s.substr(0, i), t.substr(-i)) &&
f(s.substr(i), t.substr(0, t.length-i)))
return set(s,t,true);
if (f(s.substr(0, i), t.substr(0, i)) &&
f(s.substr(i), t.substr(i)))
return set(s,t,true);
}
return set(s,t,false);
}
return f(s1, s2);
};
function test(f) {
[
["great", "great"],
["great", "eatgr"],
["great", "rgeat"],
["great", "rgtae"],
["great", "rtgea"],
["dsanujiiqwfsysnfsrwbrfhhpqicbw", "dabbciwqphhfrwrsfnsysfwqiijuns"],
["pcighfdjnbwfkohtklrecxnooxyipj", "npodkfchrfpxliocgtnykhxwjbojie"],
["iydzdwbqbfixognqhbmimhwyhmdnrm", "nmbywdbnmmfybqqighdriizmxdhwho"],
].forEach(function (input) {
console.log(f.apply(undefined, input));
});
}
if (DEBUG) test(isScramble);