Skip to content

Practice Implement Magic Dictionary

Read the task, work through the examples, and check your approach against the constraints. Use the original LeetCode task for coding and submissions.

Medium#676
LeetCode Problem

Implement Magic Dictionary

Problem Statement

MagicDictionary.buildDict(dictionary) initializes distinct lowercase words once. search(searchWord) returns true only if replacing exactly one character makes the query equal to a stored word. Insertion, deletion, and zero replacements do not qualify. Practice approach: Compare only equal-length words and count differing positions, stopping after the second mismatch. A trie can instead explore one alternate letter at one depth. An exact match alone must not pass. Test equal words, one mismatch, multiple mismatches, and different lengths.

Source: Implement Magic Dictionary on LeetCode. Use the original task for its coding environment and submissions.

Examples

# Example 1

Input
["MagicDictionary","buildDict","search","search","search","search"]
[[],[["cat","dog"]],["cat"],["cot"],["cats"],["dig"]]
Output
[null,null,false,true,false,true]
Explanation

cot differs from cat at one position; cats has the wrong length, and cat itself requires zero changes.

Constraints

1 <= dictionary.length <= 100
1 <= dictionary word length, searchWord.length <= 100
Words contain lowercase letters and dictionary words are distinct.
buildDict is called once before searches.
At most 100 search calls.

Related LeetCode Problems

Practice more problems similar to Implement Magic Dictionary.

Review your practice with PhantomCodeAI

After trying the problem yourself, use the app to discuss your approach, complexity, and edge cases.