-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcanConstruct.js
More file actions
22 lines (20 loc) · 852 Bytes
/
canConstruct.js
File metadata and controls
22 lines (20 loc) · 852 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
const canConstruct = (targetWord, wordBank, cache = {}) => {
if (targetWord in cache) return cache[targetWord]
//checking current target
if (targetWord === "") return true;
//iterating over wordbank for all word combinations
for (let word of wordBank) {
//checking whether target word starts with current word
if (targetWord.startsWith(word)) {
if (canConstruct(targetWord.slice(word.length), wordBank,cache)) {
cache[targetWord] = true
return true;
}
}
}
cache[targetWord] = false
return false
}
console.log(canConstruct('saiteja', ['sa', 'ja', 'teja', 'si', 'eja', 'i']))
console.log(canConstruct('eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeef', ['e', 'eee',
'eeeeeeeeee', 'eeeeeeeee', 'eeeeeee', 'eeeeeeeeeeeeeeeeeeeeeeeeeee']))