..
211. Design Add and Search Words Data Structure
Problem Description
Design a data structure that supports adding new words and finding if a string matches any previously added string.
Implement the WordDictionary class:
WordDictionary()
Initializes the object.void addWord(word)
Adds word to the data structure, it can be matched later.bool search(word)
Returns true if there is any string in the data structure that matches word or false otherwise. word may contain dots'.'
where dots can be matched with any letter.
Example:
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]
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
Constraints:
1 <= word.length <= 500
word in addWord consists lower-case English letters.
word in search consist of '.' or lower-case English letters.
At most 50000 calls will be made to addWord and search.
Solution
Trie + DFS
class TrieNode():
def __init__(self):
self.children = {}
self.is_word = False
class WordDictionary:
def __init__(self):
"""
Initialize your data structure here.
"""
self.root = TrieNode()
def addWord(self, word: str) -> None:
node = self.root
for char in word:
if char not in node.children:
node.children[char] = TrieNode()
node = node.children[char]
node.is_word = True
def search(self, word: str) -> bool:
return self._find(self.root, word, 0)
def _find(self, node, word, start_index):
if node is None:
return False
if start_index >= len(word):
return node.is_word
if word[start_index] != ".":
return self._find(node.children.get(word[start_index], None), word, start_index + 1)
# current char is `.`
for next_char in node.children:
if self._find(node.children.get(next_char, None), word, start_index + 1):
return True
return False
# Your WordDictionary object will be instantiated and called as such:
# obj = WordDictionary()
# obj.addWord(word)
# param_2 = obj.search(word)