# 290.Word Pattern

**290.Word-Pattern**

难度:Easy

> 给定一种 pattern(模式) 和一个字符串 str ，判断 str 是否遵循相同的模式。

这里的遵循指完全匹配，例如， pattern 里的每个字母和字符串 str 中的每个非空单词之间存在着双向连接的对应模式。

示例1:

```
输入: pattern = "abba", str = "dog cat cat dog"
输出: true
示例 2:

输入:pattern = "abba", str = "dog cat cat fish"
输出: false
示例 3:

输入: pattern = "aaaa", str = "dog cat cat dog"
输出: false
示例 4:

输入: pattern = "abba", str = "dog dog dog dog"
输出: false
说明:
你可以假设 pattern 只包含小写字母， str 包含了由单个空格分隔的小写字母。
```

代码：

```
class Solution {
public:
    bool wordPattern(string pattern, string str) {
        unordered_map<char,int>mp;
        unordered_map<string,int>ms;
        for(int i=0;i<pattern.length();i++)
            mp[pattern[i]]++;
        vector<string> vs;
        vs.push_back("");
        int t=0;
        for(int i=0;i<str.length();i++)
        {
            if(str[i] == ' ')
            {
                vs.push_back("");
                t++;
            }
            else
                vs[t]+=str[i];
        }
        for(int i=0;i<vs.size();i++)
            ms[vs[i]]++;
        if(vs.size() != pattern.length()) return false;
        for( int i=0;i<vs.size();i++)
            if(mp[pattern[i]] !=ms[vs[i]]) return false;
        return true;

    }
};
```


---

# Agent Instructions: 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://dfine.gitbook.io/leetcode/290.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.
