forked from adarsh-gupta101/Dynamic-Programming
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathallConstruct.js
More file actions
25 lines (23 loc) · 701 Bytes
/
Copy pathallConstruct.js
File metadata and controls
25 lines (23 loc) · 701 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
const allConstruct = (target, wordBank,memo={}) => {
if (target === "") return [[]];
if (target in memo) return memo[target];
const result = [];
for (let word of wordBank) {
if (target.indexOf(word) === 0) {
const suffix = target.slice(word.length);
const suffixWays = allConstruct(suffix, wordBank);
const targetWays = suffixWays.map((way) => [word, ...way]);
result.push(...targetWays);
}
}
memo[target]= result;
return result
};
// console.log(allConstruct("abcdef",["ab","cde","f","cdef"]))
console.log(
allConstruct("eeeeeeeeeeeeeeeeeeeejeeeeeeeeeeeeeeeeee", [
"eeeeeeeeeeeeeeeeeeeeeeeeeeee",
"eeeeeee",
"eeeeeeeeeeeeeeeeeee"
])
);