-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathinterleaving-string.js
More file actions
40 lines (36 loc) · 939 Bytes
/
interleaving-string.js
File metadata and controls
40 lines (36 loc) · 939 Bytes
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
// Given s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2.
//
// Example 1:
//
//
// Input: s1 = "aabcc", s2 = "dbbca", s3 = "aadbbcbcac"
// Output: true
//
//
// Example 2:
//
//
// Input: s1 = "aabcc", s2 = "dbbca", s3 = "aadbbbaccc"
// Output: false
//
//
/**
* @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
var dp = {}
var _isInterleave = function(s1, s2, s3) {
if (s1.length === 0) return s2 === s3
if (s2.length === 0) return s1 === s3
var key = s1.length + '_' + s2.length
if (dp[key] !== undefined) return dp[key]
dp[key] = (s1[0] === s3[0] && _isInterleave(s1.substr(1), s2, s3.substr(1))) ||
(s2[0] === s3[0] && _isInterleave(s1, s2.substr(1), s3.substr(1)))
return dp[key]
}
return _isInterleave(s1, s2, s3)
};