Design Add and Search Words Data Structure
MediumP3Coding
Design a dictionary that supports adding words and searching with wildcards.
WordDictionary()creates an empty dictionary.addWord(word)adds a lower-case word.search(word)returnstrueif some stored word matchesword. Inword, the character.stands for any single letter. A match must have exactly the same length asword.
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]
Constraints
- 1 <= word.length <= 25
- addWord words are lower-case; search words are lower-case or contain '.'
- At most 2 dots per search word
- At most 10^4 calls in total
Hints
Give it a real attempt first. Run shows which examples fail.
Saved automatically
Run checks the 2 visible tests, Submit all 5. ⌘Enter runs, ⌘⇧Enter submits.