mediumTrieTrie

Design Add and Search Words Data Structure

mediumTime: O(m)Space: O(m)

Signals to notice

dictionary with wildcard searchdot matches any charactertrie with DFS for wildcards

Brute force first

Store words in a list, for each search check every word with regex — per search. That instinct is useful because it follows the prompt literally, but it usually keeps revisiting work the problem is begging you to organize.

The key insight

Trie for addWord. For search: if the character is a letter, follow that branch. If it's '.', try ALL children branches (DFS). A word exists if any branch reaches an end-marked node. worst case, but typically much faster. The goal is not to be clever for its own sake, but to remember the one relationship that keeps the solution grounded as you move forward.

What must stay true

The '.' wildcard means you must explore all possible branches at that position. This turns a simple trie walk into a DFS — but the trie still prunes impossible paths at non-wildcard positions. If that remains true after every update, the rest of the reasoning has a stable place to stand.

Easy way to go wrong

Trying to search iteratively — the wildcard requires branching, which needs recursion or a stack. Iterative search only works for exact character matches. Most mistakes here are not about syntax; they come from losing track of what your state, pointer, or structure is supposed to mean.

Trie Pattern