Skip to content

Commit 187fe93

Browse files
authored
Update 1639-number-of-ways-to-form-a-target-string-given-a-dictionary.js
1 parent 1e2e1d4 commit 187fe93

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed

1639-number-of-ways-to-form-a-target-string-given-a-dictionary.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,31 @@
1+
/**
2+
* @param {string[]} words
3+
* @param {string} target
4+
* @return {number}
5+
*/
6+
const numWays = function (words, target) {
7+
const m = words[0].length, len = words.length
8+
const n = target.length, a = 'a'.charCodeAt(0)
9+
const mod = 10 ** 9 + 7
10+
const dp = Array(n).fill(0)
11+
for(let i = 0; i < m; i++) {
12+
const freq = Array(26).fill(0)
13+
for(let j = 0; j < len; j++) {
14+
freq[words[j].charCodeAt(i) - a]++
15+
}
16+
for(let j = Math.min(i, n - 1); j >= 0; j--) {
17+
const code = target[j].charCodeAt(0) - a
18+
if(freq[code] > 0) {
19+
dp[j] += (j === 0 ? freq[code] : dp[j - 1] * freq[code])
20+
dp[j] %= mod
21+
}
22+
}
23+
}
24+
return dp[n - 1]
25+
}
26+
27+
// another
28+
129
/**
230
* @param {string[]} words
331
* @param {string} target

0 commit comments

Comments
 (0)