# x 212. Word Search II

{% embed url="<https://leetcode.com/problems/word-search-ii/description/>" %}

Should be able to implement Trie with below

[Java](/overview/2.-data-structures/trie/java.md)

[Javascript](/overview/2.-data-structures/trie/javascript.md)

```javascript
/**
 * @param {character[][]} board
 * @param {string[]} words
 * @return {string[]}
 */

let dict;
let answer;
var findWords = function (board, words) {

    const visited = new Array(board.length);
    for (let i = 0; i < board.length; i++) {
        visited[i] = new Array(board[0].length).fill(0);
    }

    dict = new Trie();
    dict.insertAll(words);

    answer = new Set();
    for (let i = 0; i < board.length; i++) {
        for (let j = 0; j < board[0].length; j++) {
            const hasWord = checkWord(board, visited, i, j);
        }
    }

    return [...answer];
};

function checkWord(board, visited, row = 0, col = 0, word = []) {
    if (row < 0 || col < 0) return false;
    if (row >= board.length || col >= board[0].length) return false;
    if (visited[row][col] === 1) return false;

    const c = board[row][col];
    word = [...word, ...c];
    const [hasPrefix, done] = dict.startsWith(word);
    // console.log({
    //     c,
    //     word,
    //     hasPrefix,
    //     done
    // })

    if (!hasPrefix) return false;
    if (done === true) {
        answer.add(word.join(''));
    }


    visited[row][col] = 1;

    const up = checkWord(board, visited, row - 1, col, word);
    const down = checkWord(board, visited, row + 1, col, word);
    const right = checkWord(board, visited, row, col + 1, word);
    const left = checkWord(board, visited, row, col - 1, word);

    visited[row][col] = 0;
    return up || down || right || left;
}

const ALPHABET_SIZE = 26;
class TrieNode {

    constructor(parent, val, level = 0) {
        this.parent = parent;
        this.val = val;
        this.level = level;
        this.children = new Array(ALPHABET_SIZE).fill(null);
    }

}

class Trie {

    constructor(root) {
        this.root = root ?? new TrieNode(null);
        this.isEndOfWord = false;
    }

    insert(s) {
        let currentNode = this.root;

        let idx;
        for (let i = 0; i < s.length; i++) {
            const c = s[i];

            idx = c.charCodeAt() - 'a'.charCodeAt();
            if (currentNode.children[idx] === null) {
                currentNode.children[idx] = new TrieNode(currentNode, c, currentNode.level + 1);
            }

            currentNode = currentNode.children[idx];
        }

        currentNode.isEndOfWord = true;
    }

    search(s) {
        let currentNode = this.root;

        let idx;
        for (let i = 0; i < s.length; i++) {
            const c = s[i];
            idx = c.charCodeAt() - 'a'.charCodeAt();
            if (currentNode.children[idx] == null)
                return false;
            currentNode = currentNode.children[idx];
        }

        return currentNode.isEndOfWord;
    }

    startsWith(s) {
        let currentNode = this.root;

        let idx;
        for (let i = 0; i < s.length; i++) {
            const c = s[i];
            idx = c.charCodeAt() - 'a'.charCodeAt();
            if (currentNode.children[idx] == null)
                return [false, currentNode.isEndOfWord];
            currentNode = currentNode.children[idx];
        }

        return [true, currentNode.isEndOfWord];
    }

    insertAll(arr) {
        for (const s of arr) {
            this.insert(s);
        }
    }


}
```


---

# 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://algorithm.prettylog.com/top-75-leetcode-questions-to-save-your-time/problems/tree/x-212.-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.
