💡
Exercise 150

Add & Search Words 25 XP Medium

LeetCode Ctrl+Enter Run Ctrl+S Save

Design a data structure supporting addWord and search. The search can contain '.' which matches any single character.

Trie + DFS. When we encounter '.', try all children recursively.

💡 Pro tip: Understand this problem deeply — don't just memorize the code. Try explaining the approach out loud as if teaching a friend. If you can explain it simply, you truly understand it!

📋 Instructions
Add words ['bad','dad','mad'], then search: 'pad'→False, 'bad'→True, '.ad'→True, 'b..'→True
In search DFS: if char is '.', loop through all children and recurse. If any returns True, return True.
🧪 Test Cases
Input
wd.search('pad')
Expected
False
Boolean check
Input
wd.search('bad')
Expected
True
Boolean check
Input
wd.search('.ad')
Expected
True
Boolean check
Input
wd.search('b..')
Expected
True
Boolean check
main.py
Hi! I'm Rex 👋
Output
Ready. Press ▶ Run or Ctrl+Enter.