Design Add and Search Words Data Structure
Design a data structure supporting addWord(word) and search(word). search must return true if any previously added word matches word, where a '.' in the query can match any single letter. Non-dot characters must match exactly.
Open official problem prompt ↗Support insertion of words and matching queries where '.' is a single-character wildcard, answering each query against the whole dictionary.
Like searching a filing cabinet where some letters of the word you want are smudged: for a clear letter you open exactly one labeled drawer, but for a smudge you must peek into every drawer at that level and see if any leads to a complete match.
- Input
- addWord("bad"); addWord("dad"); addWord("mad"); search("pad"); search("bad"); search(".ad"); search("b..")
- Output
- [null, null, null, false, true, true, true]
- Why
- 'pad' was never added; 'bad' matches exactly; '.ad' matches bad/dad/mad via the wildcard; 'b..' matches 'bad' since the two dots match 'a' and 'd'.
1 <= word.length <= 25word in addWord consists of lowercase English lettersword in search consists of '.' or lowercase English lettersThere will be at most 2 dots in a search word (typical constraint)At most 10^4 calls to addWord and search