Design Add and Search Words Data Structure
Problem Statement
Design a data structure that supports adding new words and finding if a string matches any previously added string. Implement the WordDictionary class: WordDictionary() Initializes the object. void addWord(word) Adds word to the data structure, it can be matched later. bool search(word) Returns true if there is any string in the data structure that matches word or false otherwise. word may contain dots '.' where dots can be matched with any letter.
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 3 dots in word for search.
- At most 10^4 calls will be made to addWord and search.
Input Format:
- Commands to execute operations on WordDictionary data structure.
Output Format:
- Results of operations performed on the WordDictionary data structure.
Examples:
Example 1:
Input:
Input ["WordDictionary","addWord","addWord","addWord","search","search","search","search"] [[],["bad"],["dad"],["mad"],["pad"],["bad"],["..."],["..d"]]
Output:
Output [null,null,null,null,false,true,true,true]
Explanation:
WordDictionary wordDictionary = new WordDictionary(); wordDictionary.addWord("bad"); wordDictionary.addWord("dad"); wordDictionary.addWord("mad"); wordDictionary.search("pad"); // return False wordDictionary.search("bad"); // return True wordDictionary.search(".ad"); // return True wordDictionary.search("..d"); // return True
Solutions
Trie with DFS Search
Use a trie to store words and implement search with DFS to handle wildcard characters.
Array-based Trie Implementation
Use arrays instead of hash maps for the trie nodes to potentially improve performance for lowercase English letters.
Algorithm Walkthrough
Example input: nums = []
Step-by-step execution:
- Initialize a new WordDictionary with an empty root node
- Add "bad":
- Create trie nodes for 'b', 'a', 'd'
- Mark the node for 'd' as the end of a word
- Add "dad":
- Create trie node for 'd' (first letter)
- Reuse node for 'a' (second letter)
- Reuse node for 'd' (third letter)
- Mark the node for 'd' as the end of a word
- Add "mad":
- Create trie node for 'm' (first letter)
- Reuse node for 'a' (second letter)
- Reuse node for 'd' (third letter)
- Mark the node for 'd' as the end of a word
- Search "pad":
- No trie node for 'p', return false
- Search "bad":
- Follow the path 'b' -> 'a' -> 'd'
- The node for 'd' is marked as the end of a word
- Return true
- Search ".ad":
- For '.', must check all children of the root
- Check 'b' -> 'a' -> 'd', the node for 'd' is marked as the end of a word, return true
- Search "..d":
- For the first '.', check all children of the root
- For the second '.', check all children of each first-level node
- Eventually find path 'b'/'d'/'m' -> 'a' -> 'd' where the final 'd' is marked as the end of a word
- Return true
Hints
Hint 1
Hint 2
Hint 3
Video Tutorial
Video tutorials can be a great way to understand algorithms visually
Visualization
Visualize the trie data structure and how words with wildcards are searched in it.
Key visualization elements:
- current node
- wildcard expansion
- matching paths
- end of word markers
Similar Questions
Implement Trie (Prefix Tree)
MediumA trie (pronounced as "try") or prefix tree is a tree data structure used to efficiently store and retrieve keys in a dataset of strings. There are various applications of this data structure, such as autocomplete and spellchecker. Implement the Trie class: Trie() Initializes the trie object. void insert(String word) Inserts the string word into the trie. boolean search(String word) Returns true if the string word is in the trie (i.e., was inserted before), and false otherwise. boolean startsWith(String prefix) Returns true if there is a previously inserted string word that has the prefix prefix, and false otherwise.
Word Search II
HardGiven an m x n board of characters and a list of strings words, return all words on the board. Each word must be constructed from letters of sequentially adjacent cells, where adjacent cells are horizontally or vertically neighboring. The same letter cell may not be used more than once in a word.
Implementation Notes
This problem builds on the basic trie data structure by adding wildcard matching capability, making it more complex and powerful for word searching applications.