number-of-valid-words-for-each-puzzle

This commit is contained in:
Gleb Koval 2021-11-09 02:07:53 +00:00
parent 1b2f15ec8e
commit b2cb398c71
No known key found for this signature in database
GPG Key ID: 7C89CDC822F8392B
1 changed files with 30 additions and 0 deletions

View File

@ -0,0 +1,30 @@
package main
func mask(str string) int {
mask := 0
for _, r := range str {
mask |= 1 << (r - 'a')
}
return mask
}
func findNumOfValidWords(words []string, puzzles []string) []int {
word_masks := make(map[int]int)
for _, word := range words {
mask := mask(word)
word_masks[mask] += 1
}
out := make([]int, len(puzzles))
for pi, puzzle := range puzzles {
pc := mask(puzzle[1:])
fc := 1 << (puzzle[0] - 'a')
count, submask := word_masks[fc], pc
for submask != 0 {
count += word_masks[submask|fc]
submask = (submask - 1) & pc
}
out[pi] = count
}
return out
}