Advertisement

Number of Valid Words for Each Puzzle - LeetCode 1178 Solution

Number of Valid Words for Each Puzzle - Complete Solution Guide

Number of Valid Words for Each Puzzle is LeetCode problem 1178, a Hard level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.

Problem Statement

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). Return an array answer , where answer[i] is the number of words in the given word list w

Detailed Explanation

The problem asks us to determine, for each puzzle in a given list of puzzles, how many words from a given word list are 'valid' with respect to that puzzle. A word is considered valid if it satisfies two conditions: 1) It contains the first letter of the puzzle, and 2) Every letter in the word is also present in the puzzle. Both words and puzzles consist of lowercase English letters. We need to return an array where each element at index 'i' represents the count of valid words for the puzzle at index 'i' in the puzzles list. Crucially, each puzzle has a length of 7 and contains no repeated characters.

Solution Approach

The solution employs a bit manipulation approach. First, each word in the `words` array is converted into a bitmask, where the i-th bit is set if the i-th letter of the alphabet ('a' + i) is present in the word. Then, a hash map (or equivalent data structure) is used to store the count of each unique word mask. For each puzzle, a similar bitmask is created, and all possible submasks of the puzzle's characters (excluding the first character) are generated. For each submask, the first character's bit is added to it to form a potential 'word mask'. If this 'word mask' exists in the hash map, the count of valid words for that puzzle is incremented. Finally, the counts for all puzzles are returned as a list.

Step-by-Step Algorithm

  1. Step 1: Create a hash map `word_counts` to store the count of each unique word mask.
  2. Step 2: Iterate through each word in `words`. For each word, create a bitmask representing the presence of each character in the word. Update `word_counts` with this mask and its count.
  3. Step 3: Initialize an empty list `answer` to store the counts of valid words for each puzzle.
  4. Step 4: Iterate through each puzzle in `puzzles`. For each puzzle:
  5. Step 5: Extract the bitmask corresponding to the first character of the puzzle.
  6. Step 6: Extract the bitmask corresponding to the remaining 6 characters of the puzzle.
  7. Step 7: Initialize a count variable `count` to 0.
  8. Step 8: Generate all possible submasks of the 6-character mask. This is done by iterating from the 6-character mask down to 0, and using the bitwise AND operator to ensure each submask only contains bits present in the original 6-character mask.
  9. Step 9: For each submask, combine it with the first character's mask to create a potential word mask.
  10. Step 10: If this potential word mask exists in `word_counts`, increment the `count` by the number of times the word mask occurs.
  11. Step 11: Add the `count` to the `answer` list.
  12. Step 12: Return the `answer` list.

Key Insights

  • Insight 1: The limited size of puzzles (length 7) and words being lowercase letters suggests using bit manipulation to represent words and puzzles as integers where each bit corresponds to a letter.
  • Insight 2: The core idea is to precompute a bitmask for each word and then, for each puzzle, efficiently check which words are valid by generating submasks of the puzzle's characters (excluding the first character) and combining them with the first character's mask.
  • Insight 3: Generating all possible submasks of a 6-character string (excluding the first letter) has a complexity of O(2^6), which is acceptable given the constraints.

Complexity Analysis

Time Complexity: O(p * 2^6 + n)

Space Complexity: O(n)

Topics

This problem involves: Array, Hash Table, String, Bit Manipulation, Trie.

Companies

Asked at: Dropbox.