-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path097.js
More file actions
37 lines (33 loc) · 1.1 KB
/
097.js
File metadata and controls
37 lines (33 loc) · 1.1 KB
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
/**
* @param {string} s1
* @param {string} s2
* @param {string} s3
* @return {boolean}
*/
var isInterleave = function(s1, s2, s3) {
if (s1.length + s2.length !== s3.length) return false;
let cache = {};
for (let length1 = 0; length1 <= s1.length; ++length1) {
cache[length1] = {};
}
for (let length1 = 0; length1 <= s1.length; ++length1) {
for (let length2 = 0; length2 <= s2.length; ++length2) {
if (length1 === 0 && length2 === 0) {
cache[length1][length2] = true;
continue;
}
if (length1 === 0) {
cache[length1][length2] = cache[length1][length2 - 1] && s2[length2 - 1] === s3[length2 - 1];
continue;
}
if (length2 === 0) {
cache[length1][length2] = cache[length1 - 1][length2] && s1[length1 - 1] === s3[length1 - 1];
continue;
}
// length1 > 0 and length2 > 0
cache[length1][length2] = (cache[length1 - 1][length2] && s1[length1 - 1] === s3[length1 + length2 - 1])
|| (cache[length1][length2 - 1] && s2[length2 - 1] === s3[length1 + length2 - 1]);
}
}
return cache[s1.length][s2.length];
};