-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcountConstruct.js
22 lines (20 loc) · 921 Bytes
/
countConstruct.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
const countConstruct = (targetWord, wordBank,cache={}) => {
if (targetWord in cache) return cache[targetWord]
//checking current target
if (targetWord === "") return 1;
//iterating over wordbank for all word combinations
let totalWays = 0
for (let word of wordBank) {
//checking whether target word starts with current word
if (targetWord.startsWith(word)) {
const numberOfWay = countConstruct(targetWord.slice(word.length), wordBank,cache)
totalWays += numberOfWay
}
}
cache[targetWord] = totalWays
return totalWays
}
console.log(countConstruct('saiteja', ['sa', 'ja', 'teja', 'si', 'eja', 'i']))
console.log(countConstruct('purple', ['pu', 'p', 'le', 'ur', 'rple']))
console.log(countConstruct('eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeef', ['e', 'eee',
'eeeeeeeeee', 'eeeeeeeee', 'eeeeeee', 'eeeeeeeeeeeeeeeeeeeeeeeeeee']))