Implement Magic Dictionary - Problem
Implement Magic Dictionary - Create a Smart Word Matching System

Design a data structure that acts like a "magic dictionary" - it can find words that are almost matches! Given a list of dictionary words, your magic dictionary should be able to determine if you can change exactly one character in a search word to match any word in the dictionary.

Key Requirements:
MagicDictionary() - Initialize the object
buildDict(dictionary) - Build the dictionary from an array of distinct strings
search(searchWord) - Return true if changing exactly one character in searchWord matches any dictionary word

Example: If dictionary contains ["hello", "leetcode"], then searching for "hhllo" returns true (change 'h' to 'e'), but "hello" returns false (no changes needed).

Input & Output

example_1.py — Basic Magic Dictionary
$ Input: magicDictionary = MagicDictionary() magicDictionary.buildDict(["hello", "leetcode"]) magicDictionary.search("hello") magicDictionary.search("hhllo") magicDictionary.search("hell") magicDictionary.search("leetcoded")
Output: [null, null, false, true, false, false]
💡 Note: buildDict builds the dictionary. search('hello') returns false (no changes needed, must change exactly one). search('hhllo') returns true (change first 'h' to 'e' to get 'hello'). search('hell') and search('leetcoded') return false due to different lengths.
example_2.py — Single Character Words
$ Input: magicDictionary = MagicDictionary() magicDictionary.buildDict(["a", "b"]) magicDictionary.search("a") magicDictionary.search("b") magicDictionary.search("c") magicDictionary.search("ab")
Output: [null, null, false, false, true, false]
💡 Note: Dictionary has ['a', 'b']. search('a') and search('b') return false (exact matches, no change). search('c') returns true (can change 'c' to 'a' or 'b'). search('ab') returns false (different length).
example_3.py — Multiple Word Matches
$ Input: magicDictionary = MagicDictionary() magicDictionary.buildDict(["hello", "hallo", "hullo"]) magicDictionary.search("hxllo")
Output: [null, null, true]
💡 Note: Dictionary has words that differ only in the second character. search('hxllo') returns true because changing 'x' to 'e', 'a', or 'u' matches dictionary words.

Visualization

Tap to expand
Magic Dictionary: Finding the Perfect Imperfect Match🎯 Goal: Find if changing exactly 1 character in "hhllo" matches any dictionary wordDictionary: ["hello", "leetcode"] | Search: "hhllo"Step-by-Step Trie Traversal:1. Start at root, looking for 'h'root→ 'h' found ✓2. Next character 'h', but Trie has 'e'h🔄eMISMATCH! Use our 1 allowed change3. Continue with remaining 'llo' - all match!Path: root → h → e → l → l → o ✓Result: Found "hello" with exactly 1 change → return TRUE💡 Key InsightTrie + DFS allows efficient exploration of all possible one-character modifications
Understanding the Visualization
1
Build Dictionary
Construct Trie from dictionary words ['hello', 'leetcode']
2
Start Search
Begin DFS search for 'hhllo' with 1 mismatch allowed
3
Handle Mismatch
At position 1: 'h' doesn't match 'e', use our one allowed mismatch
4
Continue Match
Remaining characters 'llo' match exactly in Trie path
5
Found Result
Reached end of word with exactly 0 mismatches remaining → return true
Key Takeaway
🎯 Key Insight: The Trie approach efficiently explores exactly one character modification by using DFS with a mismatch counter, achieving optimal time complexity while handling the constraint elegantly.

Time & Space Complexity

Time Complexity
⏱️
O(n*m) build, O(m*26) search

Build: insert n words of length m. Search: DFS with at most 26 branches per level

n
2n
Linear Growth
Space Complexity
O(ALPHABET_SIZE * N * M)

Trie nodes for storing dictionary words with shared prefixes

n
2n
Linear Space

Constraints

  • 1 ≤ dictionary.length ≤ 100
  • 1 ≤ dictionary[i].length ≤ 100
  • dictionary[i] consists of only lower-case English letters
  • All the strings in dictionary are distinct
  • 1 ≤ searchWord.length ≤ 100
  • searchWord consists of only lower-case English letters
  • buildDict will be called only once before search
  • At most 100 calls will be made to search
Asked in
Google 42 Amazon 28 Microsoft 18 Meta 15
89.2K Views
Medium Frequency
~25 min Avg. Time
1.8K Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen