Implement Magic Dictionary - Problem

Design a data structure that is initialized with a list of different words. Given a string, determine if you can change exactly one character in this string to match any word in the data structure.

Implement the MagicDictionary class:

  • MagicDictionary() - Initializes the object
  • buildDict(String[] dictionary) - Sets the data structure with an array of distinct strings dictionary
  • search(String searchWord) - Returns true if you can change exactly one character in searchWord to match any string in the data structure, otherwise returns false

Input & Output

Example 1 — Basic Operations
$ Input: operations = ["MagicDictionary", "buildDict", "search", "search"], inputs = [[], ["hello", "leetcode"], "hello", "hhllo"]
Output: [null, null, false, true]
💡 Note: Initialize dictionary with ["hello", "leetcode"]. Search "hello" returns false (can't change to itself). Search "hhllo" returns true (change 'h' to 'e' makes "hello")
Example 2 — Multiple Words
$ Input: operations = ["MagicDictionary", "buildDict", "search", "search"], inputs = [[], ["hello", "hallo"], "hello", "hillo"]
Output: [null, null, true, true]
💡 Note: Dictionary has ["hello", "hallo"]. Search "hello" returns true (change 'e' to 'a' makes "hallo"). Search "hillo" returns true (change 'i' to 'e' makes "hello")
Example 3 — No Match
$ Input: operations = ["MagicDictionary", "buildDict", "search"], inputs = [[], ["hello"], "hellllo"]
Output: [null, null, false]
💡 Note: Dictionary has ["hello"]. Search "hellllo" returns false (different lengths, cannot match with exactly one change)

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

Visualization

Tap to expand
Magic Dictionary - Hash Map with Patterns INPUT Dictionary Words: "hello" "leetcode" Pattern Map: *ello --> ["hello"] h*llo --> ["hello"] he*lo --> ["hello"] l*etcode --> ["leetcode"] ... more patterns Search Words: "hello" "hhllo" ALGORITHM STEPS 1 Build Pattern Map For each word, create patterns by replacing each char with * 2 Generate Search Patterns Create patterns from searchWord e.g., "hhllo" --> "*hllo", "h*llo" 3 Match Patterns Look up patterns in map Check for matching words 4 Verify Exactly One Change Ensure matched word differs by exactly one character Example: search("hhllo") "h*llo" in map? Yes! map["h*llo"] = ["hello"] "hhllo" != "hello" --> Return true FINAL RESULT search("hello") Pattern: h*llo Match: "hello" Same word! (0 changes) false search("hhllo") Pattern: h*llo Match: "hello" Different! (1 change) true Output Array: [null,null,false,true] Key Insight: By pre-computing wildcard patterns (replacing each character with *), we can find words that differ by exactly one character in O(L) time per search, where L is the word length. The pattern map enables efficient lookups while checking that the match is not the exact same word. TutorialsPoint - Implement Magic Dictionary | Hash Map with Patterns Approach
Asked in
Google 25 Facebook 20 Amazon 15
67.0K Views
Medium Frequency
~25 min Avg. Time
892 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