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

// struct cNode {
//     char val;
//     bool isEnd;
//     string word;
//     unordered_map<char, cNode*> children;
// };

// class Trie {
//     public:
//     cNode* root;
    
//     Trie() {
//         root = new cNode();
//     }

//     void add(string s) {
//         cNode* cur = root;
//         for (auto c : s) {
//             if (cur->children.find(c) == cur->children.end()) {
//                 cur->children[c] = new cNode(c);
//             }
//             cur = cur->children[c];
//         }
//         cur->isEnd = true;
//         cur->word = s;
//     }

//     void contains(string s) {
//         cNode* cur = root;
//         for (auto c : s) {
//             if (cur->children.find(c) == cur->children.end()) {
//                 cout << "Missing!" << endl;
//             }
//             cur = cur->children[c];
//         }
//         cout << "Found!: " << cur->isEnd << " word: " << cur->word << endl;
//     }
// };

// class Solution {
// public:
//     unordered_set<string> response;

//     vector<pair<int, int>> moves = {
//         {1, 0},
//         {-1, 0},
//         {0, 1},
//         {0, -1}
//     };

//     vector<string> findWords(vector<vector<char>>& board, vector<string>& words) {
//        Trie* trie = new Trie();

//        for (auto word : words) {
//         trie->add(word);
//        }

//        for (int i = 0; i < board.size(); i++) {
//         for (int j = 0; j < board[0].size(); j++) {
//             set<pair<int, int>> visited;
//             search(i, j, board, trie->root, visited);
//         }
//        }
//        return vector(response.begin(), response.end());
//     }

//     void search(int i, int j, vector<vector<char>>& board, cNode* cur, set<pair<int, int>>& seen) {
//         if (cur->isEnd) {
//             response.insert(cur->word);
//         }

//         // If invalid return
//         if (i < 0 || i >= board.size() || j < 0 || j >= board[0].size()) return;
//         // If visited before return
//         if (seen.find({i, j}) != seen.end()) return;
//         // If not in trie return
//         if (cur->children.find(board[i][j]) == cur->children.end()) return;
//         // cout << "checking " << board[i][j] << " cur->val " << cur->val << " cur->isEnd " << cur->isEnd << endl;

//         seen.insert({i, j});
//         for (auto move : moves) {
//             int m_i = i + move.first;
//             int m_j = j + move.second;
//             search(m_i, m_j, board, cur->children[board[i][j]], seen);
//         }
//         seen.erase({i, j});
//         if (cur->children.size() == 0) {
//             cur->children.erase(board[i][j]);
//         }
//     }
// };
struct Node {
    vector<Node*> children;
    bool isEnd;
    
    Node() {
        children = vector<Node*>(150, NULL);
        isEnd = false;
    }
};

class Trie {
public:
    
    Node* root;
    
    /** Initialize your data structure here. */
    Trie() {
        root = new Node();      
    }
    
    /** Inserts a word into the trie. */
    void insert(string word) {
        Node* cur = root;
        
        for (auto c : word) {
            if (cur->children[c] == NULL) {
                cur->children[c] = new Node();
            }
            cur = cur->children[c];
        }
        cur->isEnd = true;
    }
    
    Node* getRoot() {
        return root;
    }
};

class Solution {
public:
    vector<string> findWords(vector<vector<char>>& board, vector<string>& words) {
        Trie* trie = new Trie();
        
        // 1. Add all words to trie
        for (auto word : words) {
            trie->insert(word);
        }
        
        // 2. Iterate over board and check each letter against trie
        set<string> seen;
        for (auto r = 0; r < board.size(); r++) {
            for (auto c = 0; c < board[0].size(); c++) {
                helper(board, r, c, seen, trie->getRoot(), "");
            }
        }
        
        // 3. Convert seen set to vector
        vector<string> result;
        for (auto it : seen) {
            result.push_back(it);
        }
        return result;
    }
    
    void helper(vector<vector<char>>& board, int r, int c, set<string>& seen, Node* root, string word) {
        if (!(r >= 0 && c >= 0 && r < board.size() && c < board[0].size() && board[r][c] != ' ')) return;
        
        char curChar = board[r][c];
        if (root->children[curChar] != NULL) {
            word += curChar;
            root = root->children[curChar];       
            if (root->isEnd) {
                seen.insert(word);
            }
            board[r][c] = ' ';
            helper(board, r + 1, c, seen, root, word);
            helper(board, r, c + 1, seen, root, word);
            helper(board, r - 1, c, seen, root, word);
            helper(board, r, c - 1, seen, root, word);
            board[r][c] = curChar;
        }
    }
};
38106