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

# 单词规律

题目链接: <https://leetcode.cn/problems/word-pattern>

## 解题思路：

1. 遵循的前提是pattern中每个出现的字符都可以映射到s中的单词，且同一个字符不能映射到不同的单词身上
2. 因此需要先对s按空格拆分字符串数组得到sList
3. 再遍历字符串pattern，建立pattern每个字符字符与sList的每个单词的映射表以及sList每个单词与s的每个字符的映射表
4. 当出现sList中的某个单词被pattern中的两个字符映射或pattern中的某个字符映射到了sList中的两个单词时，则判定为不遵循

```go
func wordPattern(pattern string, s string) bool {
	sList := strings.Split(s, " ")
	if len(pattern)!=len(sList){
		return false
	}
	p2s := map[byte]string{}
	s2p := map[string]byte{}
	for i := range pattern {
		x, y := pattern[i], sList[i]
		if (p2s[x] != "" && p2s[x] != y) || (s2p[y] > 0 && s2p[y] != x) {
			return false
		}
		p2s[x] = y
		s2p[y] = x
	}
	return true
}
```

## 复杂度分析

* **时间复杂度：** 时间复杂度是 $$O(n)$$
* **空间复杂度：** 空间复杂度是 $$O(n)$$


---

# 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://leetcodebook-1.gitbook.io/top-interview-150/hash-table/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.
