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

# Find and Replace Pattern Python Solution

> Tested Python solution for LeetCode 890 with 18 pytest cases. Generate a practice environment with lcpy.

LeetCode 890, [Medium](/catalog/medium). Topics: [Array](/catalog/topics/array), [Hash Table](/catalog/topics/hash-table), [String](/catalog/topics/string). [View on LeetCode](https://leetcode.com/problems/find-and-replace-pattern/description/).

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

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

## Problem

Given a list of strings `words` and a string `pattern`, return *a list of* `words[i]` *that match* `pattern`. You may return the answer in **any order**.

A word matches the pattern if there exists a permutation of letters `p` so that after replacing every letter `x` in the pattern with `p(x)`, we get the desired word.

Recall that a permutation of letters is a bijection from letters to letters: every letter maps to another letter, and no two letters map to the same letter.

### Examples

```
Input: words = ["abc","deq","mee","aqq","dkd","ccc"], pattern = "abb"
Output: ["mee","aqq"]
Explanation: "mee" matches the pattern because there is a permutation {a -> m, b -> e, ...}.
"ccc" does not match the pattern because {a -> c, b -> c, ...} is not a permutation, since a and b map to the same letter.
```

```
Input: words = ["a","b","c"], pattern = "a"
Output: ["a","b","c"]
```

### Constraints

* `1 <= pattern.length <= 20`
* `1 <= words.length <= 50`
* `words[i].length == pattern.length`
* `pattern` and `words[i]` are lowercase English letters.

## Solution

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

```python theme={"theme":{"light":"github-light","dark":"github-dark"}}
class Solution:
    # Time: O(n * m) where n = len(words), m = len(pattern)
    # Space: O(m)
    def find_and_replace_pattern(self, words: list[str], pattern: str) -> list[str]:
        def matches(word: str) -> bool:
            if len(word) != len(pattern):
                return False
            p_to_w: dict[str, str] = {}
            w_to_p: dict[str, str] = {}
            for pc, wc in zip(pattern, word, strict=True):
                if p_to_w.setdefault(pc, wc) != wc or w_to_p.setdefault(wc, pc) != pc:
                    return False
            return True

        return [word for word in words if matches(word)]
```

## Complexity

| Time | Space |
| - | - |
| O(n \* m) where n = len(words), m = len(pattern) | O(m) |

## Tags


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