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

# 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.
2. pattern =`"abba"`, str =`"dog cat cat fish"`

   should return false.
3. pattern =`"aaaa"`, str =`"dog cat cat dog"`

   should return false.
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.

```
class Solution(object):
    def wordPattern(self, pattern, str):
        """
        :type pattern: str
        :type str: str
        :rtype: bool
        """
        if not pattern and not str:
            return True

        # since str has words separated by spaces    
        str_arr = str.split()
        if len(str_arr) != len(pattern):
            return False

        dict = {}
        start = 0
        for idx in xrange(len(pattern)):
            ch = pattern[idx]
            substr = ''
            #/////////////////////////////////////////
            # easy to obtain substr when the str has ' ' (space) separating words
            #/////////////////////////////////////////
            for idx2 in xrange(start, len(str)):
                if str[idx2] != ' ':
                    substr += str[idx2]
                else:
                    start = idx2 + 1
                    break;

            #print 'ch: ', ch, ' substr: ', substr, ' start: ', start
            if ch not in dict:
                if substr not in dict.values():
                    #print 'hashing: ', ch, ' -> ', substr
                    dict[ch] = substr
                else:
                    return False

            else:
                if dict[ch] != substr:
                    return False

        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:

```
GET https://bhabs.gitbook.io/prepbook/coding/word-pattern.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
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.
