<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 Solution {

class Trie{
private:
    int v;
    int pv;
    vector<Trie* > children; 

public:
    Trie() : v(0), pv(0), children(26) {}

    void insert(const string& word){
        Trie *root = this;
        for ( int i = 0; i < word.length(); i++ ){
            int idx = word[i] - 'a';
            if (!root->children[idx]){
                root->children[idx] = new Trie();
            }
            root->children[idx]->pv++;
            if ( i == word.size() -1 )
                root->children[idx]->v++;
            root = root->children[idx];
        }
    }

    bool search(const string& word){
        Trie *root = this;
        for ( int i = 0; i < word.length(); i++ ){
            int idx = word[i] - 'a';
            if ( !root->children[idx]){
                return false;
            }
            if ( i == word.length()-1 && root->children[idx]->v > 0){
                return true;
            }
            root = root->children[idx];
        }
        return false;
    }

    bool remove(const string& word){
        Trie *root = this;

        if ( !this->search(word) )
            return false;

        for ( int i = 0; i < word.length(); i++ ){
            int idx = word[i] - 'a';
            if ( root->children[idx]->pv <= 0)
                return false;

            root->children[idx]->pv--;

            if ( i == word.length()-1 ) {
                if ( root->children[idx]->v <= 0)
                    return false;
                root->children[idx]->v--;
            }
            root = root->children[idx];
        }
        return true;
    }

    bool startsWith(string prefix) {
        Trie *trie = this;
        for ( int i = 0; i < prefix.size(); i++ ){
            int idx = prefix[i] - 'a';
            if ( !trie->children[idx]  )
                return false;

            if ( i == prefix.size() - 1  && trie->children[idx]->pv > 0 )
                return true;

            trie = trie->children[idx];
        }
        return false;
    }
};

public:

    struct hashFunction 
    { 
        size_t operator()(const pair<bool,  
                            bool> &x) const
        { 
            return x.first ^ x.second; 
        } 
    }; 

    vector<string> findWords(vector<vector<char>>& board, vector<string>& words) {

        // make a Trie
        Trie *trie = new Trie();
        for ( int i = 0; i < words.size(); i++ ){
            trie->insert(words[i]);
        }

        vector<string> result;
        unordered_set<pair<int, int>, hashFunction> is_path;
        // search && backtrack
        for ( int i = 0; i < board.size(); i++ ){
            for ( int j = 0; j < board[0].size(); j++ ){
                backtrack(trie, board, i, j, is_path, result, string(1, board[i][j]));
            }
        }
        return result;       
    }

    void backtrack(Trie *trie, vector<vector<char>>& board, int x, int y, unordered_set<pair<int, int>, hashFunction>& is_path, vector<string>& result, string word){
        
        if ( !trie || !trie->startsWith(word) || is_path.contains(pair(x,y)))
            return;

        if ( trie->remove(word) ){
            result.push_back(word);
        }

        is_path.insert(pair(x,y));

        if (x > 0 ) backtrack(trie, board, x-1, y, is_path, result, word+board[x-1][y]);
        if (x < board.size()-1 ) backtrack(trie, board, x+1, y, is_path, result, word+board[x+1][y]);
        if (y > 0 ) backtrack(trie, board, x, y-1, is_path, result, word+board[x][y-1]);
        if (y < board[0].size()-1 ) backtrack(trie, board, x, y+1, is_path, result, word+board[x][y+1]);

        is_path.erase(pair(x,y));
    }
};

74691