> 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/trie/word-search-ii.md).

# 单词搜索 II

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

## 解题思路：

1. 对于所有的`words`构建一个前缀树，前缀树的每个节点对应一个单词，每个单词的最后一个字母对应一个节点，单词的其余部分对应子节点
2. 遍历`board`，判断每个位置出发的四个方向是否能走到前缀树的叶子节点，若能走到，则覆盖该单词，记录已遍历
3. 对于从一个位置出发后，则将该位置标记为'#'，避免重复遍历，四个方向遍历完之后，则将该位置复原

```go
type Trie struct {
	subTrie []*Trie
	word    string
}

func contract() *Trie {
	return &Trie{
		subTrie: make([]*Trie, 26),
	}
}

func (t *Trie) Insert(word string) {
	node := t
	for _, ch := range word {
		ch -= 'a'
		if node.subTrie[ch] == nil {
			node.subTrie[ch] = contract()
		}
		node = node.subTrie[ch]
	}
	node.word = word
}

func findWords(board [][]byte, words []string) []string {
	if words == nil || len(words) == 0 || board == nil || len(board) == 0 {
		return nil
	}
	result := make([]string, 0)
	root := contract()
	directs := [][]int{{-1, 0}, {1, 0}, {0, -1}, {0, 1}}
	for _, word := range words {
		root.Insert(word)
	}
	m, n := len(board), len(board[0])
	seen := map[string]bool{}
	var dfs func(x int, y int, t *Trie)
	dfs = func(x int, y int, trie *Trie) {
		ch := board[x][y]
		trie = trie.subTrie[ch - 'a']
		if trie == nil {
			return
		}
		if trie.word != "" {
			seen[trie.word] = true
		}
		board[x][y] = '#'
		for _, direct := range directs {
			tmpx, tmpy := x+direct[0], y+direct[1]
			if tmpx >= 0 && tmpx < m && tmpy >= 0 && tmpy < n && board[tmpx][tmpy] != '#' {
				dfs(tmpx, tmpy, trie)
			}
		}
		board[x][y] = ch
	}
	for i, row := range board {
		for j := range row {
			dfs(i, j, root)
		}
	}

	for key, _ := range seen {
		result = append(result, key)
	}
	return result
}
```

## 复杂度分析

* **时间复杂度：** 时间复杂度为$$O(m\*n)$$
* **空间复杂度：** 空间复杂度为$$O(m\*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, and the optional `goal` query parameter:

```
GET https://leetcodebook-1.gitbook.io/top-interview-150/trie/word-search-ii.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.
