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
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)
โ Linear Growth
Space Complexity
O(n*m)
Store n words, each of average length m
โก 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
๐ก
Explanation
AI Ready
๐ก Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code