Design Add and Search Words Data Structure - Problem

Design a smart word dictionary that can store words and search for them with a twist - it supports wildcard matching!

Your task is to implement a WordDictionary class with these capabilities:

  • Add words: Store new words in your data structure
  • Search with wildcards: Find words where dots (.) can match any single letter

For example, if you add the word "bad", then searching for "b.d" should return true because the dot can match the 'a'.

Class Interface:

WordDictionary() - Initialize the data structure
void addWord(word) - Add a word to the dictionary
bool search(word) - Search for a word (with wildcard support)

The challenge is to efficiently handle both exact matches and wildcard patterns while maintaining good performance for both operations.

Input & Output

example_1.py โ€” Basic Operations
$ Input: WordDictionary wd = new WordDictionary(); wd.addWord("bad"); wd.addWord("dad"); wd.addWord("mad"); wd.search("pad"); // false wd.search("bad"); // true wd.search(".ad"); // true wd.search("b.."); // true
โ€บ Output: [null, null, null, null, false, true, true, true]
๐Ÿ’ก Note: We add three words and test various search patterns. 'pad' doesn't exist, 'bad' exists exactly, '.ad' matches 'bad', 'dad', 'mad', and 'b..' matches 'bad'.
example_2.py โ€” Edge Cases
$ Input: WordDictionary wd = new WordDictionary(); wd.addWord("a"); wd.addWord("a"); wd.search("."); // true wd.search("a"); // true wd.search("aa"); // false wd.search(".a"); // false wd.search("a."); // false
โ€บ Output: [null, null, null, true, true, false, false, false]
๐Ÿ’ก Note: Adding duplicate words is allowed. Single character 'a' matches '.', length mismatches return false.
example_3.py โ€” Complex Wildcards
$ Input: WordDictionary wd = new WordDictionary(); wd.addWord("word"); wd.addWord("work"); wd.addWord("world"); wd.search("wor."); // true (matches 'word', 'work') wd.search("wor.d"); // true (matches 'world') wd.search("w..ld"); // true (matches 'world') wd.search(".o.ld"); // true (matches 'world')
โ€บ Output: [null, null, null, null, true, true, true, true]
๐Ÿ’ก Note: Multiple wildcards in different positions all work correctly, matching various stored words based on the pattern.

Visualization

Tap to expand
Trie-Based Dictionary with Wildcard SearchROOTbdmaooadygdStored Words: "bad", "boy", "dog", "mad"Search Pattern: "b.d"โœ“ MATCH: bโ†’aโ†’dโœ— No match: bโ†’oโ†’yWildcard Algorithm:1. Start at root node2. For each character:โ€ข If exact char: follow pathโ€ข If '.': try ALL children3. Check if word ends here4. Return true if match found
Understanding the Visualization
1
Build the Trie
Each word creates a path from root, sharing common prefixes
2
Handle Exact Matches
For regular characters, follow the single path in the trie
3
Handle Wildcards
For '.' characters, recursively explore all possible child paths
4
Check End State
Verify we've consumed entire pattern and reached a valid word ending
Key Takeaway
๐ŸŽฏ Key Insight: Trie structure enables efficient prefix sharing and the recursive DFS approach elegantly handles wildcard expansion by exploring all possible character matches at each '.' position.

Time & Space Complexity

Time Complexity
โฑ๏ธ
O(n*m)

For search: n words ร— m characters per comparison. Add is O(1)

n
2n
โœ“ Linear Growth
Space Complexity
O(n*m)

Store n words, each of average length m

n
2n
โšก Linearithmic Space

Constraints

  • 1 โ‰ค word.length โ‰ค 25
  • word in addWord consists of lowercase English letters
  • word in search consist of '.' or lowercase English letters
  • There will be at most 2 ร— 104 calls to addWord and search
  • Wildcards '.' can match any single lowercase letter
Asked in
Google 45 Amazon 38 Microsoft 32 Meta 28 Apple 22
42.6K Views
High Frequency
~25 min Avg. Time
1.6K 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