Number of Valid Words for Each Puzzle - Problem

Given an array of words and an array of puzzles, return an array answer where answer[i] is the number of words that are valid with respect to the puzzle puzzles[i].

With respect to a given puzzle string, a word is valid if both the following conditions are satisfied:

  • word contains the first letter of puzzle.
  • For each letter in word, that letter is in puzzle.

For example, if the puzzle is "abcdefg", then valid words are "faced", "cabbage", and "baggage", while invalid words are "beefed" (does not include 'a') and "based" (includes 's' which is not in the puzzle).

Input & Output

Example 1 — Basic Case
$ Input: words = ["aaaa","asas","able","ability","actt","actor","access"], puzzles = ["aboveyz","abrodyz","abslute","absoryz","actresz","gaswxyz"]
Output: [1,1,3,2,4,0]
💡 Note: For puzzle "aboveyz": word "aaaa" contains 'a' and all letters are in puzzle, so count=1. For "abslute": words "able", "ability", "actt" are valid, so count=3.
Example 2 — Single Letters
$ Input: words = ["apple","pleas","please"], puzzles = ["aelpxy","aelpxy","aelpsxy"]
Output: [0,1,3]
💡 Note: "apple" doesn't contain first letter 'a' from puzzle letters only. "pleas" is valid for second puzzle. All three words valid for third puzzle.
Example 3 — No Valid Words
$ Input: words = ["word","test"], puzzles = ["abcdef"]
Output: [0]
💡 Note: Neither "word" nor "test" contains 'a' (first letter of puzzle), so no valid words.

Constraints

  • 1 ≤ words.length ≤ 105
  • 4 ≤ words[i].length ≤ 50
  • 1 ≤ puzzles.length ≤ 104
  • puzzles[i].length == 7
  • words[i] and puzzles[i] consist of lowercase English letters
  • Each puzzles[i] contains unique characters

Visualization

Tap to expand
Valid Words for Each Puzzle INPUT words[] = "aaaa" "asas" "able" "ability" "actt" "actor" "access" puzzles[] = "aboveyz" "abrodyz" "abslute" "absoryz" "actresz" "gaswxyz" Validity Rules: 1. Word must contain puzzle's first letter 2. Every letter in word must be in puzzle "able" valid for "abslute" (a,b,l,e in puzzle) ALGORITHM STEPS 1 Bitmask Words Convert each word to 26-bit mask, store count "able" --> 0...010011 (a,b,e,l) 2 Store in HashMap Map: bitmask --> count Handle duplicate masks 3 Process Each Puzzle Get puzzle's bitmask Note first letter bit 4 Enumerate Subsets Iterate all submasks of puzzle that include 1st char sub = (sub - 1) & puzzleMask if (sub & firstBit) count += map[sub] Time: O(W*L + P*2^7) W=words, P=puzzles, L=word length FINAL RESULT Output Array: [1, 1, 3, 2, 4, 0] Breakdown by Puzzle: "aboveyz" --> 1 (aaaa) "abrodyz" --> 1 (aaaa) "abslute" --> 3 (aaaa,able,ability) "absoryz" --> 2 (aaaa,asas) "actresz" --> 4 (aaaa,actt,actor,access) "gaswxyz" --> 0 (no valid words) OK - All Puzzles Processed! Key Insight: Since puzzles have at most 7 unique letters, we enumerate all 2^7 = 128 subsets containing the first letter. Using bitmasks to represent character sets allows O(1) subset checking and efficient storage in a HashMap. This approach avoids checking each word against each puzzle directly (which would be O(W*P*L)). TutorialsPoint - Number of Valid Words for Each Puzzle | Optimal Bitmask Solution
Asked in
Google 45 Facebook 32 Amazon 28
28.5K Views
Medium Frequency
~35 min Avg. Time
892 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen