# 290. 单词模式

<https://leetcode-cn.com/problems/word-pattern/>

## 解法一：

类似于205.同构字符串，用两个hash表，每次给对应的pattern和str一个唯一的数字标识，一旦数字对不上，说明匹配失败。

pattern是字符串，可以用数字代替hashmap。每个字符的asc码作为键值，作为256长度的数组中的下标，值为数字标识。

str是多个串，按空格split之后存在一个list中，由于串长度不一定为1，不方便再像上面那样用数组表示，因此用一个map，键为字符串，值为数字标识。

```python
class Solution:
    def wordPattern(self, pattern: str, str: str) -> bool:
        strList = str.split(' ')
        if len(pattern) != len(strList):    #避免数量不匹配的case
            return False
        dict_p = [-1] * 256
        dict_s = {}
        for i in range(len(pattern)):
            if dict_p[ord(pattern[i])] == -1:
                dict_p[ord(pattern[i])] = i
            if strList[i] not in dict_s:
                dict_s[strList[i]] = i
            if dict_p[ord(pattern[i])] != dict_s[strList[i]]:
                return False
        return True
```
