-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCanConstruct.js
More file actions
60 lines (57 loc) · 1.88 KB
/
CanConstruct.js
File metadata and controls
60 lines (57 loc) · 1.88 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
// m = target.length
// n = wordBank.length
//Recursion
/*const canConstruct =(target, wordBank) => { // T.C. = O(n^m*m^2) S.C. = O(m^2)
if(target==='') return true;
let suffix;
for(let word of wordBank) {
if(target.indexOf(word)===0) {
suffix=target.slice(word.length);
if(canConstruct(suffix,wordBank))
return true;
}
}
return false;
}*/
//Memoization
/*const canConstruct =(target, wordBank, memo={}) => { // T.C. = O(n*m^2) S.C = O(m^2)
if(target in memo) return memo[target];
if(target==='') return true;
let suffix;
for(let word of wordBank) {
if(target.indexOf(word)===0) {
suffix=target.slice(word.length);
if(canConstruct(suffix,wordBank,memo))
return true;
}
}
memo[target]=false;
return false;
}*/
//Tabulation
const canConstruct =(target, wordBank) => { // T.C. = O(n*m^2) S.C. = O(m)
const table= Array(target.length+1).fill(false);
table[0]=true;
for(i=0;i<=target.length;i++) {
if(table[i]) {
for(word of wordBank)
if(!table[i+word.length]&&target.slice(i,i+word.length)===word) // target.slice(i).indexOf(word)===0
table[i+word.length]=true;
}
}
// console.log(target);
// console.log(table);
return table[target.length];
}
console.log(canConstruct("abcdef",["ab","abc","cd","def","abcd"])); // true
console.log(canConstruct("skateboard",["bo","rd","ate","t","ska","sk","boar"])); // false
console.log(canConstruct("enterapotentopt",["a","p","ent","enter","ot","o","t"])); // true
console.log(canConstruct("amangupta",["am","ama","ta","gup","an"])); // true
console.log(canConstruct("eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeef", [
"e",
"ee",
"eee",
"eeeee",
"eeeeee",
"eeeeeeee",
"eeeeeeeeee"])); // false