1324. Number of Valid Words for Each Puzzle

EasyBit ManipulationBit ManipulationBitmaskSubset

Given a list of words and a list of puzzles, a word is valid for a puzzle if it contains the puzzle's first letter and every letter of the word appears in the puzzle. Return an array giving, for each puzzle, the number of valid words. Each word and puzzle is represented as a 26-bit letter mask.

Input: A JSON object {"words": [<strings>], "puzzles": [<strings>]}. Each puzzle is a string of distinct lowercase letters.

Output: Return an array of counts, one per puzzle, in the same order.

Examples

Example 1
Input: {"words":["aaaa","asas","able","ability","actt","actor","access"],"puzzles":["aboveyz","abrodyz","abslute","absoryz","actresz","gaswxyz"]}
Output: [1,1,3,2,4,0]
Explanation: Counts of valid words per puzzle are [1,1,3,2,4,0].
Example 2
Input: {"words":["apple","pleas","please"],"puzzles":["aelwxyz","aelpxyz","aelpsxy"]}
Output: [0,1,3]
Explanation: Only puzzles whose letters cover a word (and include its first letter) count it.

Constraints

Asked by

TCSInfosysWiproCognizantCapgemini
Solve this problem in the editor →