Wildcard Word Dictionary
Store words in a trie and search them with patterns where a dot stands for any one letter.
The problem
Design a class WordDictionary. WordDictionary() creates an empty dictionary. addWord(word) stores a lowercase word. search(word) returns True if some stored word matches word, otherwise False. The search word may contain dots: each '.' matches exactly one letter of any kind, and every other letter must match itself, so a match always has the same length as the search word. Words are at most 25 letters long, a search word has at most two dots, and there can be up to 10,000 calls.
Starter code
class WordDictionary:
def __init__(self):
passPublic examples
obj = WordDictionary() obj.addWord("bad") obj.addWord("dad") obj.addWord("mad") obj.search("pad") obj.search("bad") obj.search(".ad") obj.search("b..")gives[None, None, None, None, False, True, True, True]obj = WordDictionary() obj.addWord("car") obj.addWord("cat") obj.search("ca") obj.search("car")gives[None, None, None, False, True]obj = WordDictionary() obj.addWord("abc") obj.search("a..") obj.search("b..")gives[None, None, True, False]
Solving it in the practice book needs an active paid plan.
Open it in the practice bookOne thought at a time.
