Skip to content

Commit 24cc3b1

Browse files
authored
Create 1178-number-of-valid-words-for-each-puzzle.js
1 parent 522ff5e commit 24cc3b1

File tree

1 file changed

+33
-0
lines changed

1 file changed

+33
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/**
2+
* @param {string[]} words
3+
* @param {string[]} puzzles
4+
* @return {number[]}
5+
*/
6+
const findNumOfValidWords = function(words, puzzles) {
7+
let n = puzzles.length,
8+
offset = 'a'.charCodeAt()
9+
let res = new Array(n).fill(0)
10+
let cnt = {}
11+
12+
for (let w of words) {
13+
let mask = 0
14+
for (let c of w) {
15+
mask |= 1 << (c.charCodeAt() - offset)
16+
}
17+
cnt[mask] = ~~cnt[mask] + 1
18+
}
19+
for (let i = 0; i < n; i++) {
20+
let s = puzzles[i],
21+
len = s.length
22+
for (let k = 0; k < 1 << (len - 1); k++) {
23+
let mask = 1 << (s[0].charCodeAt() - offset)
24+
for (let j = 0; j < len - 1; j++) {
25+
if (k & (1 << j)) {
26+
mask |= 1 << (s[j + 1].charCodeAt() - offset)
27+
}
28+
}
29+
res[i] += ~~cnt[mask]
30+
}
31+
}
32+
return res
33+
}

0 commit comments

Comments
 (0)