Design Add and Search Words Data Structure
Design a data structure that stores words and searches them with wildcards.
addWord(word)inserts one lowercase word.search(word)returnstruewhen a stored word matches the whole pattern.- In a search pattern,
.matches any single lowercase English letter.
Examples
Input: [["WordDictionary","addWord","addWord","search","search","search"],[[],["bad"],["dad"],["pad"],["bad"],[".ad"]]]
Output: [null,null,null,false,true,true]
Input: [["WordDictionary","addWord","search"],[[],["a"],["a"]]]
Output: [null,null,true]
Hints
Store words in a trie so shared prefixes are traversed once.
At a `.` in `search`, branch into every child; at a letter, follow only that child.
Mark terminal nodes so `search` distinguishes an inserted word from a prefix.
Related Problems
Design Add and Search Words Data Structure
Design a data structure that stores words and searches them with wildcards.