Design a class WordDictionary that stores words and answers pattern searches:
add_word(word)storesword(adding the same word twice has no extra effect).search(pattern)returnsTrueif some stored word matchespatternexactly, where the character.in the pattern matches any single letter.
Examples
ops: ["WordDictionary", "add_word", "add_word", "add_word", "search", "search", "search", "search"]
args: [[], ["bad"], ["dad"], ["mad"], ["pad"], ["bad"], [".ad"], ["b.."]]
Output: [None, None, None, None, False, True, True, True]
Constraints
- Words and patterns have length
1..25; words are lowercase letters; patterns are lowercase letters or. - Up to
10**4calls in total - Target:
add_wordinO(len(word));searchexplores only trie branches consistent with the pattern
Goals
- Store words in a trie inside a class
- Branch on every child when the pattern character is a wildcard