# Word Dictionary
https://leetcode.com/problems/design-add-and-search-words-data-structure/
Similar to [[trie]]. Can be implemented with `trie`.
```python
class WordDictionary(defaultdict):
def __init__(self):
super().__init__(WordDictionary)
self.is_end = False
def __bool__(self):
return bool(super())
def addWord(self, word: str) -> None:
node = self
for c in word:
node = node[c]
node.is_end = True
def search(self, word: str) -> bool:
node = self
for i, c in enumerate(word):
if c == '.':
return any(
node.search(prefix + word[i + 1:])
for prefix in node.keys()
)
if not (node := node.get(c)):
return False
return node.is_end
```
Similar to [[trie]], `addWord` can be simplified.
## Improved
```python
class WordDictionary(dict):
def addWord(self, word: str) -> None:
reduce(lambda node, c: node.setdefault(c, {}), word, self)['