-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCountConstruct.js
More file actions
61 lines (58 loc) · 1.89 KB
/
CountConstruct.js
File metadata and controls
61 lines (58 loc) · 1.89 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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
// m = target.length
// n = wordBank.length
//Recursion
/*const countConstruct =(target, wordBank) => { // T.C. = O(n^m*m^2) S.C. = O(m^2)
if(target==='') return 1;
let suffix;
let count=0;
for(let word of wordBank) {
if(target.indexOf(word)===0) {
suffix=target.slice(word.length);
count+=countConstruct(suffix,wordBank);
}
}
return count;
}*/
//Memoization
/*const countConstruct =(target, wordBank, memo={}) => { // T.C. = O(n*m^2) S.C = O(m^2)
if(target in memo) return memo[target];
if(target==='') return 1;
let suffix;
let count=0;
for(let word of wordBank) {
if(target.indexOf(word)===0) {
suffix=target.slice(word.length);
count+=countConstruct(suffix,wordBank,memo);
}
}
memo[target]=count;
return count;
}*/
//Tabulation
const countConstruct =(target,wordBank) => { // T.C. = O(n*m^2) S.C. = O(m)
const table=Array(target.length+1).fill(0);
table[0]=1;
for(i=0;i<=target.length;i++) {
if(table[i]!=0) {
for(word of wordBank) {
if(target.slice(i,i+word.length)===word) { // target.slice(i).indexOf(word)===0
table[i+word.length]+=table[i];
}
}
}
}
return table[target.length];
}
console.log(countConstruct("abcdef",["ab","abc","cd","def","abcd"])); // 1
console.log(countConstruct("skateboard",["bo","rd","ate","t","ska","sk","boar"])); // 0
console.log(countConstruct("enterapotentpot",["a","p","ent","enter","ot","o","t"])); // 4
console.log(countConstruct("purple",["purp","p","ur","le","purpl"])); // 2
console.log(countConstruct("amangupta",["am","ama","ta","gup","an","n","a"])); // 3
console.log(countConstruct("eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeef", [
"e",
"ee",
"eee",
"eeeee",
"eeeeee",
"eeeeeeee",
"eeeeeeeeee"])); // 0