Skip to content

Practice Number of Valid Words for Each Puzzle

Read the task, work through the examples, and check your approach against the constraints. Use the original LeetCode task for coding and submissions.

Hard#1178
LeetCode Problem

Number of Valid Words for Each Puzzle

Problem Statement

For each puzzle, count the words that use only letters found in that puzzle and also contain its first letter. A letter may repeat inside a word. Return one count per puzzle, retaining the puzzle order and counting each qualifying word-list entry. Each puzzle contains seven distinct lowercase letters. Practice approach: Convert each word to a 26-bit letter mask and count identical masks. A mask with more than seven set bits cannot qualify. For a puzzle, enumerate the subsets of its six noninitial letters and combine each with the required initial-letter bit. Sum their stored frequencies. This checks 64 masks per puzzle instead of scanning every word again.

Source: Number of Valid Words for Each Puzzle on LeetCode. Use the original task for its coding environment and submissions.

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]

# Example 2

Input
words = ["apple","pleas","please"], puzzles = ["aelwxyz","aelpxyz","aelpsxy","saelpxy","xaelpsy"]
Output
[0,1,3,2,0]

Constraints

1 <= words.length <= 10^5
4 <= words[i].length <= 50
1 <= puzzles.length <= 10^4
puzzles[i].length == 7
words[i] and puzzles[i] consist of lowercase English letters.
Each puzzles[i] does not contain repeated characters.

Related LeetCode Problems

Practice more problems similar to Number of Valid Words for Each Puzzle.

Review your practice with PhantomCodeAI

After trying the problem yourself, use the app to discuss your approach, complexity, and edge cases.