# 212. Word Search II

### Description

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.

**Note:**

1. All inputs are consist of lowercase letters `a-z`.
2. The values of `words` are distinct.

### Constraints

### Approach

### Links

* GeeksforGeeks
* [Leetcode](https://leetcode.com/problems/word-search-ii/)
* ProgramCreek
* YouTube

### **Examples**

{% tabs %}
{% tab title="Example 1" %}

<div align="left"><img src="/files/-MJbuPM5ENhyKbzP2g0T" alt=""></div>
{% endtab %}
{% endtabs %}

### **Solutions**

{% tabs %}
{% tab title="Solution 1" %}

```java
/**
 * Time complexity : 
 * Space complexity : 
 */

class Solution {
    
    class TrieNode {
        TrieNode[] children = new TrieNode[26];
        String word = null;
    }
    
    public List<String> findWords(char[][] board, String[] words) {
        List<String> result = new ArrayList();
        if(board == null || board.length == 0) {
            return result;
        }
        TrieNode trieNode = buildTrie(words);
        int row = board.length,
            col = board[0].length;
        for(int r = 0; r < row; r++) {
            for(int c = 0; c < col; c++) {
                dfs(board, r, c, trieNode, result);
            }
        }
        return result;
    }
    
    private void dfs(char[][] board,
                     int r,
                     int c,
                     TrieNode trieNode,
                     List<String> result
                    ) {
        int row = board.length,
            col = board[0].length;
        if(r < 0 || r >= row || c < 0 || c >= col) return; 
        if(board[r][c] == '#' || 
           trieNode.children[board[r][c]-'a'] == null) return;
        
        char ch = board[r][c];
        trieNode = trieNode.children[board[r][c]-'a'];
        
        if(trieNode.word != null) {
            result.add(trieNode.word);
            trieNode.word = null;
        }
        board[r][c] = '#';
        dfs(board, r+1, c, trieNode, result);
        dfs(board, r-1, c, trieNode, result);
        dfs(board, r, c+1, trieNode, result);
        dfs(board, r, c-1, trieNode, result);
        board[r][c] = ch;
    }
    
    private TrieNode buildTrie(String[] words) {
        TrieNode trieNode = new TrieNode();
        for(String word: words) {
            TrieNode currNode = trieNode;
            for(char ch: word.toCharArray()) {
                if(currNode.children[ch-'a'] == null) {
                    currNode.children[ch-'a'] = new TrieNode();
                }
                currNode = currNode.children[ch-'a'];
            }
            currNode.word = word;
        }
        return trieNode;
    }
}
```

{% endtab %}
{% endtabs %}

### **Follow up**

*


---

# 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://code-snippets.hbamithkumara.com/leetcode/problems/201-300/word-search-ii.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.
