> For the complete documentation index, see [llms.txt](https://maksimdan.gitbook.io/interview-practice-problems/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://maksimdan.gitbook.io/interview-practice-problems/leetcode_sessions/290-word-pattern.md).

# 290 Word Pattern

Given a`pattern`and a string`str`, find if`str`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`str`.

**Examples:**

1. pattern = `"abba"`, str = `"dog cat cat dog"` should return `true`. &#x20;
2. pattern = `"abba"`, str = `"dog cat cat fish"` should return `false`. &#x20;
3. pattern = `"aaaa"`, str = `"dog cat cat dog"` should return `false`. &#x20;
4. pattern = `"abba"`, str = `"dog dog dog dog"` should return `false`.

**Notes:**\
You may assume`pattern`contains only lowercase letters, and`str`contains lowercase letters separated by a single space.

**The Idea:** Use a bidirectional map to check for a one to one mapping between each character in the pattern and each word in the sentence.

**Complexity:** O(n) time and O(2n) space:

```python
def wordPattern(self, pattern, str):
    """
    :type pattern: str
    :type str: str
    :rtype: bool
    """
    if len(pattern) != len(str.split()):
        return False
    chars_word = {}
    word_char = {}
    for char, word in zip(pattern, str.split()):
        if (chars_word.__contains__(char) and chars_word[char] != word)\
                or word_char.__contains__(word) and word_char[word] != char:
            return False
        elif not chars_word.__contains__(char):
            chars_word[char] = word
            word_char[word] = char

    return True
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://maksimdan.gitbook.io/interview-practice-problems/leetcode_sessions/290-word-pattern.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
