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

# Word Pattern Python Solution with Tests

> Tested Python solution for LeetCode 290 with 20 pytest cases. Generate a practice environment with lcpy.

LeetCode 290, [Easy](/catalog/easy). Topics: [Hash Table](/catalog/topics/hash-table), [String](/catalog/topics/string). [View on LeetCode](https://leetcode.com/problems/word-pattern/description/).

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

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

## Problem

Given a `pattern` and a string `s`, find if `s` follows the same pattern.

Here **follow** means a full match, such that there is a bijection between a letter in `pattern` and a **non-empty** word in `s`. Specifically:

* Each letter in `pattern` maps to **exactly** one unique word in `s`.
* Each unique word in `s` maps to **exactly** one letter in `pattern`.
* No two letters map to the same word, and no two words map to the same letter.

### Examples

```
Input: pattern = 'abba', s = 'dog cat cat dog'
Output: true
Explanation:
The bijection can be established as:
- 'a' maps to 'dog'.
- 'b' maps to 'cat'.
```

```
Input: pattern = 'abba', s = 'dog cat cat fish'
Output: false
```

```
Input: pattern = 'aaaa', s = 'dog cat cat dog'
Output: false
```

### Constraints

* 1 \<= pattern.length \<= 300
* `pattern` contains only lower-case English letters.
* 1 \<= s.length \<= 3000
* `s` contains only lowercase English letters and spaces `' '`.
* `s` does not contain any leading or trailing spaces.
* All the words in `s` are separated by a single space.

## Solution

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

```python theme={"theme":{"light":"github-light","dark":"github-dark"}}
class Solution:
    # Time: O(n)  # n = number of words in s
    # Space: O(n)
    def word_pattern(self, pattern: str, s: str) -> bool:
        words = s.split()
        if len(pattern) != len(words):
            return False
        char_to_word: dict[str, str] = {}
        word_to_char: dict[str, str] = {}
        for char, word in zip(pattern, words, strict=True):
            if char in char_to_word:
                if char_to_word[char] != word:
                    return False
            elif word in word_to_char:
                return False
            else:
                char_to_word[char] = word
                word_to_char[word] = char
        return True
```

## Complexity

| Time | Space |
| - | - |
| O(n)  # n = number of words in s | O(n) |

## Tags

[NeetCode All](/catalog/neetcode).


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