The Count Words Obtained After Adding a Letter interview question gives you two lists of strings: startWords and targetWords. For each word in targetWords, you want to see if it can be formed by:
startWords.targetWords can be obtained this way.Google uses this Hash Table and Bit Manipulation interview pattern to evaluate your ability to handle sets and permutations efficiently. Since the order of letters doesn't matter, it's about set membership. The challenge is to avoid O(N*M) string comparisons and use an O(N+M) approach with clever encoding.
The problem is best solved using Bitmasking or Sorting with a Hash Set.
startWord as a 26-bit integer (bitmask). Store all start masks in a Hash Set.targetWord, generate its bitmask.targetWord, create a "candidate mask" by removing that character's bit. If this candidate mask exists in the startWords set, the target is obtainable.startWords = ["abc"], targetWords = ["abcd", "abce"]
0...0111 (bits for a, b, c). Set: {7}.Bitmasks are perfect for problems where character order doesn't matter and the alphabet size is small (<= 32 or 64). They allow for O(1) set operations and very low memory usage.
| Title | Difficulty | Topics | LeetCode |
|---|---|---|---|
| Find And Replace in String | Medium | Solve | |
| Group Anagrams | Medium | Solve | |
| Alert Using Same Key-Card Three or More Times in a One Hour Period | Medium | Solve | |
| Before and After Puzzle | Medium | Solve | |
| Groups of Special-Equivalent Strings | Medium | Solve |