<p>Given an <code>m x n</code> <code>board</code>&nbsp;of characters and a list of strings <code>words</code>, return <em>all words on the board</em>.</p>

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

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/11/07/search1.jpg" style="width: 322px; height: 322px;" />
<pre>
<strong>Input:</strong> board = [[&quot;o&quot;,&quot;a&quot;,&quot;a&quot;,&quot;n&quot;],[&quot;e&quot;,&quot;t&quot;,&quot;a&quot;,&quot;e&quot;],[&quot;i&quot;,&quot;h&quot;,&quot;k&quot;,&quot;r&quot;],[&quot;i&quot;,&quot;f&quot;,&quot;l&quot;,&quot;v&quot;]], words = [&quot;oath&quot;,&quot;pea&quot;,&quot;eat&quot;,&quot;rain&quot;]
<strong>Output:</strong> [&quot;eat&quot;,&quot;oath&quot;]
</pre>

<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/11/07/search2.jpg" style="width: 162px; height: 162px;" />
<pre>
<strong>Input:</strong> board = [[&quot;a&quot;,&quot;b&quot;],[&quot;c&quot;,&quot;d&quot;]], words = [&quot;abcb&quot;]
<strong>Output:</strong> []
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>m == board.length</code></li>
	<li><code>n == board[i].length</code></li>
	<li><code>1 &lt;= m, n &lt;= 12</code></li>
	<li><code>board[i][j]</code> is a lowercase English letter.</li>
	<li><code>1 &lt;= words.length &lt;= 3 * 10<sup>4</sup></code></li>
	<li><code>1 &lt;= words[i].length &lt;= 10</code></li>
	<li><code>words[i]</code> consists of lowercase English letters.</li>
	<li>All the strings of <code>words</code> are unique.</li>
</ul>

class Node {
public:
    vector<Node*> children;
    char c;
    bool isEnd;

    Node(char cc) {
        c = cc;
        isEnd = false;

        for(int i = 0; i < 26; i ++) {
            children.push_back(nullptr);
        }
    }

    Node* addChild(char cc) {
        children[cc - 'a'] = new Node(cc);
        return children[cc - 'a'];
    }

    bool hasChild(char cc) {
        return children[cc - 'a'] != nullptr;
    }

    Node* getChild(char cc) {
        return children[cc - 'a'];
    }
};

class Solution {
public:
    // current node in trie, current string, current coordinates
    void backtrack(Node* node, string s, int i, int j) {
        // reached a word end, can add to answer set.
        if(node->isEnd) {
            ans.insert(s);
        }

        for(int k = 0; k < 4; k ++) {
            int dx = i + ax[k];
            int dy = j + ay[k];

            if(dx >= 0 && dx < n && dy >= 0 && dy < m && !myvis[dx][dy]) {
                if(node->hasChild(myboard[dx][dy])) {
                    myvis[dx][dy] = true;
                    backtrack(node->getChild(myboard[dx][dy]), s + myboard[dx][dy], dx, dy);
                    myvis[dx][dy] = false;
                }
            }
        }
    }
    vector<string> findWords(vector<vector<char>>& board, vector<string>& words) {
        n = board.size();
        m = board[0].size();

        myboard = board;

        root = new Node('a');
        // populate trie with words
        for(int i = 0; i < words.size(); i ++) {
            Node* node = root;
            for(int j = 0; j < words[i].size(); j ++) {
                if(node->hasChild(words[i][j])) {
                    node = node->getChild(words[i][j]);
                } else {
                    node = node->addChild(words[i][j]);
                }
            }

            node->isEnd = true;
        }

        for(int i = 0; i < n; i ++) {
            for(int j = 0; j < m; j ++) {
                if(root->hasChild(board[i][j])) {
                    vector<vector<bool>> vis(n,vector<bool>(m, false));
                    myvis = vis;
                    myvis[i][j] = true;
                    backtrack(root->getChild(board[i][j]), string(1, board[i][j]), i,j);
                }
            }
        }

        vector<string> v;

        for(auto & s: ans) {
            v.push_back(s);
        }

        return v;
    }
private:
    int n;
    int m;
    vector<vector<bool>> myvis;
    vector<vector<char>> myboard;
    Node* root;
    int ax[4] = {1,0,-1,0};
    int ay[4] = {0,1,0,-1};
    set<string> ans;
};
29976