> ## Documentation Index
> Fetch the complete documentation index at: https://leetcode-py.wisl.dev/llms.txt
> Use this file to discover all available pages before exploring further.

> ## Agent Instructions
> leetcode-py is a Python LeetCode practice environment generator with one CLI: lcpy. It is not a service or platform.
> Each problem is a directory under leetcode/ with README.md, solution.py, test_solution.py, helpers.py, and playground.ipynb. lcpy gen creates them from JSON templates bundled with the package.
> Examples are backed by tests; copy them verbatim.

# Implement Magic Dictionary Python Solution

> Tested Python solution for LeetCode 676 with 26 pytest cases. Generate a practice environment with lcpy.

LeetCode 676, [Medium](/catalog/medium). Topics: [Hash Table](/catalog/topics/hash-table), [String](/catalog/topics/string), [Depth-First Search](/catalog/topics/depth-first-search), [Design](/catalog/topics/design), [Trie](/catalog/topics/trie). [View on LeetCode](https://leetcode.com/problems/implement-magic-dictionary/description/).

Generate this problem as a practice environment: tested reference solution, 26 [parametrized pytest cases](/practice/testing), and a playground notebook:

```bash theme={"theme":{"light":"github-light","dark":"github-dark"}}
lcpy gen -n 676   # by problem number
lcpy gen -s implement_magic_dictionary   # by problem name
```

## Problem

Design a data structure that is initialized with a list of **different** words. Provided a string, you should 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.
* `void buildDict(String[] dictionary)` Sets the data structure with an array of distinct strings `dictionary`.
* `bool 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`.

### Examples

```
Input
["MagicDictionary", "buildDict", "search", "search", "search", "search"]
[[], [["hello", "leetcode"]], ["hello"], ["hhllo"], ["hell"], ["leetcoded"]]
Output
[null, null, false, true, false, false]

Explanation
MagicDictionary magicDictionary = new MagicDictionary();
magicDictionary.buildDict(["hello", "leetcode"]);
magicDictionary.search("hello"); // return False
magicDictionary.search("hhllo"); // We can change the second 'h' to 'e' to match "hello" so we return True
magicDictionary.search("hell"); // return False
magicDictionary.search("leetcoded"); // return False
```

### 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`.

## Solution

Reference implementation from [solution.py on GitHub](https://github.com/wislertt/leetcode-py/blob/main/leetcode/implement_magic_dictionary/solution.py), full suite in [test\_solution.py](https://github.com/wislertt/leetcode-py/blob/main/leetcode/implement_magic_dictionary/test_solution.py):

```python theme={"theme":{"light":"github-light","dark":"github-dark"}}
class MagicDictionary:
    # Time: build_dict O(total chars), search O(25 * n)
    # Space: O(total chars)

    def __init__(self) -> None:
        self.words: set[str] = set()

    def build_dict(self, dictionary: list[str]) -> None:
        self.words = set(dictionary)

    def search(self, search_word: str) -> bool:
        for i, kept in enumerate(search_word):
            prefix = search_word[:i]
            suffix = search_word[i + 1 :]
            for char in "abcdefghijklmnopqrstuvwxyz":
                if char != kept and prefix + char + suffix in self.words:
                    return True
        return False
```

## Complexity

| Time | Space |
| - | - |
| build\_dict O(total chars), search O(25 \* n) | O(total chars) |

## Tags


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.