> For the complete documentation index, see [llms.txt](https://wentao-shao.gitbook.io/leetcode/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://wentao-shao.gitbook.io/leetcode/graph-search/212.word-search-ii.md).

# 212.Word-Search-II

## 212. Word Search II

## 题目地址

<https://leetcode.com/problems/word-search-ii/solution/>

## 题目描述

```
Given a 2D board and a list of words from the dictionary, find all words in the board.

Each word must be constructed from letters of sequentially adjacent cell, where "adjacent" cells are those horizontally or vertically neighboring. The same letter cell may not be used more than once in a word.

Example:
Input: 
board = [
  ['o','a','a','n'],
  ['e','t','a','e'],
  ['i','h','k','r'],
  ['i','f','l','v']
]
words = ["oath","pea","eat","rain"]

Output: ["eat","oath"]
```

## 代码

### Approach #1 Backtracking with Trie

```java
class Solution {

  class TrieNode {
    HashMap<Character, TrieNode> children = new HashMap();
    String word = null;
    public TrieNode() { }
  }

  char[][] board;
  int[] dr = new int[] {0, 1, -1, 0};
  int[] dc = new int[] {1, 0, 0, -1};
  public List<String> findWords(char[][] board, String[] words) {
    TrieNode root = new TrieNode();
    this.board = board;
    for (String word: words) {
        TrieNode node = root;
        for (int i = 0; i < word.length(); i++) {
            if (node.children.containsKey(word.charAt(i))) {
                node = node.children.get(word.charAt(i));
            } else {
                TrieNode newNode = new TrieNode();
                node.children.put(word.charAt(i), newNode);
                node = newNode;
            }
        }
        node.word = word;
    }

    List<String> ans = new ArrayList();
    for (int r = 0; r < board.length; r++) {
      for (int c = 0; c < board[0].length; c++) {
        if (root.children.containsKey(board[r][c])) {
            dfs(ans, root, r, c);
        }
      }
    }
    return ans;
}

    private void dfs(List<String> ans, TrieNode parent, int row, int col) {
    Character letter = board[row][col];
    TrieNode currNode = parent.children.get(letter);

    if (currNode.word != null) {
      ans.add(currNode.word);
      currNode.word = null;
    } 

    board[row][col] = '#';
    for (int i = 0; i < 4; ++i) {
      int newRow = row + dr[i];
      int newCol = col + dc[i];
      if (newRow >= 0 && newRow < board.length && newCol >= 0
        && newCol < board[0].length && 
          currNode.children.containsKey(board[newRow][newCol])) {
          dfs(ans, currNode, newRow, newCol);
      }
    }
    board[row][col] = letter;

    }
}
```


---

# 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://wentao-shao.gitbook.io/leetcode/graph-search/212.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.
