Watch 10 video solutions for Number of Valid Words for Each Puzzle, a hard level problem involving Array, Hash Table, String. This walkthrough by Pepcoding has 16,346 views views. Want to try solving it yourself? Practice on FleetCode or read the detailed text solution.
puzzle string, a word is valid if both the following conditions are satisfied:
word contains the first letter of puzzle.word, that letter is in puzzle.
"abcdefg", then valid words are "faced", "cabbage", and "baggage", while"beefed" (does not include 'a') and "based" (includes 's' which is not in the puzzle).answer, where answer[i] is the number of words in the given word list words that is valid with respect to the puzzle puzzles[i].
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: 1 valid word for "aboveyz" : "aaaa" 1 valid word for "abrodyz" : "aaaa" 3 valid words for "abslute" : "aaaa", "asas", "able" 2 valid words for "absoryz" : "aaaa", "asas" 4 valid words for "actresz" : "aaaa", "asas", "actt", "access" There are no valid words for "gaswxyz" cause none of the words in the list contains letter 'g'.
Example 2:
Input: words = ["apple","pleas","please"], puzzles = ["aelwxyz","aelpxyz","aelpsxy","saelpxy","xaelpsy"] Output: [0,1,3,2,0]
Constraints:
1 <= words.length <= 1054 <= words[i].length <= 501 <= puzzles.length <= 104puzzles[i].length == 7words[i] and puzzles[i] consist of lowercase English letters.puzzles[i] does not contain repeated characters.Problem Overview: You receive a list of words and a list of 7‑letter puzzles. A word is valid for a puzzle if it contains the puzzle’s first letter and every character in the word appears in the puzzle. For each puzzle, return the number of valid words.
Approach 1: Brute Force Approach (O(W * P * L) time, O(1) space)
The most direct strategy checks every word against every puzzle. For each pair, verify two conditions: the word contains the puzzle’s first character, and every letter in the word exists in the puzzle. This can be implemented with sets or frequency arrays. The method is simple but expensive because you repeatedly scan words and puzzles, leading to roughly O(W * P * L) time where L is average word length. Works only for small datasets and mainly helps understand the constraints before optimizing.
Approach 2: Direct Check with Early Stopping (O(W * P) average, O(1) space)
Instead of building sets every time, convert each puzzle into a quick lookup structure such as a boolean array of size 26. While scanning a word, stop immediately if you encounter a character not in the puzzle. Also check early whether the word contains the required first letter. This reduces unnecessary work and improves practical performance, though the theoretical complexity is still close to O(W * P). Useful when implementing a straightforward optimized baseline before introducing bit manipulation.
Approach 3: Bitmask Approach (O(W * P) time, O(W) space)
Represent each word and puzzle as a 26‑bit integer mask where each bit indicates whether a letter exists. Bit operations make subset checks extremely fast. A word is valid if its mask contains the puzzle’s first letter and (wordMask & puzzleMask) == wordMask. This removes character-by-character checks and replaces them with constant-time bit operations. The technique relies heavily on bitmasking and counting masks using a hash table.
Approach 4: Bitmasking with Precomputation (O(W + P * 2^6) time, O(W) space)
The optimal solution precomputes frequencies of word masks using a hash map. Words with more than 7 unique letters can be ignored since puzzles only contain 7 characters. For each puzzle, fix the first letter and generate all subsets of the remaining six letters (there are 2^6 = 64). Combine each subset with the first letter to form candidate masks and look them up in the map. Summing their frequencies gives the valid word count for that puzzle. This dramatically reduces work and runs in roughly O(W + P * 64). Some implementations also use a trie for similar pruning, though bitmask subset enumeration is typically faster.
Recommended for interviews: The bitmasking with subset enumeration approach is the expected solution. It demonstrates understanding of bit representation, subset generation, and hash-based counting. Mentioning the brute force approach first shows baseline reasoning, but implementing the optimized bitmask solution proves you can handle high-constraint problems efficiently.
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Brute Force Check | O(W * P * L) | O(1) | Understanding constraints or very small inputs |
| Direct Check with Early Stopping | ~O(W * P) | O(1) | Simple optimized baseline without bit operations |
| Bitmask Approach | O(W * P) | O(W) | When using fast subset checks via bit operations |
| Bitmasking with Precomputation | O(W + P * 2^6) | O(W) | Optimal approach for large inputs and interview settings |