Magic Dictionary

Hard
Concept
Code
Loading editor...
Tap Analyze to see visualization
Variables

Run code to see variables

Output

Console output will appear here

Press Space to start to step? all shortcuts

Solution Guide: Magic Dictionary

Approach

Store all words in trie. During search, DFS tracks whether one mismatch has already been used.

Complexity Analysis

Time
O(L * alphabet)
Space
O(total characters)

Pattern

Trie with One-Mismatch DFS

Why It Works

The dictionary path must match exactly except one allowed mismatch. DFS explores all characters and only succeeds when length ends with exactly one change.

Updated Feb 2026