#211
Design Add and Search Words Data Structure
specialist · 895 · lc medium +31 · 48.2% accepted · 8,059 likes · top 34%
Description
Build a searchable word dictionary that supports wildcard patterns.
Implement WordDictionary:
- WordDictionary() creates the dictionary.
- void addWord(word) records word for future lookups.
- bool search(word) returns true if a stored word matches the pattern; dots '.' in the pattern match any single letter.
Example 1:
Input
["WordDictionary","addWord","addWord","addWord","search","search","search","search"]
[[],["bad"],["dad"],["mad"],["pad"],["bad"],[".ad"],["b.."]]
Output
[null,null,null,null,false,true,true,true]
Example 2:
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("b.."); // return True
Code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16