<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 TrieNode{
public:
  bool end_ = false;
  bool emplaced_ = false;
  unordered_map<char, TrieNode*> next_;
};

class Trie{
public:
  TrieNode* root_ = new TrieNode();

  void insert(const string& word){
    TrieNode* cur = root_;
    for (int i = 0; i < word.size(); ++i){
      if (!cur->next_.contains(word[i])){
        cur->next_[word[i]] = new TrieNode();
      }
      if (i == word.size() - 1){
        cur->next_[word[i]]->end_ = true;
      }
      cur = cur->next_[word[i]];
    }
  }

  void remove(const string& word){
    stack<TrieNode*> trie_node_stack;
    TrieNode* cur = root_;
    for (int i = 0; i < word.size(); ++i){
      if (!cur->next_.contains(word[i])){
        return;
      }
      trie_node_stack.push(cur);
      cur = cur->next_[word[i]];
    }
    cur->end_ = false;
    if (!cur->next_.empty()){
      return;
    }
    for (int i = word.size() - 1; i >= 0; --i){
      if (trie_node_stack.top()->next_[word[i]]->end_){
        return;
      }
      trie_node_stack.top()->next_.erase(word[i]);
      if (!trie_node_stack.top()->next_.empty()){
        return;
      }
      trie_node_stack.pop();
    }
  }
};

class Solution {
public:
  bool valid(vector<vector<char>>& board, vector<vector<bool>>& visited, int i, int j){
    if (i < 0 || i >= board.size() || j < 0 || j >= board[0].size()){
      return false;
    }
    if (visited[i][j]){
      return false;
    }
    return true;
  }
  void dfs(vector<vector<char>>& board, Trie& trie_tree, TrieNode* cur, int i, int j, vector<vector<bool>>& visited, string& str, unordered_set<string>& res){
    if (!valid(board, visited, i, j)){
      return;
    }
    // cout << i << ", " << j << ", " << board[i][j] << (cur == nullptr) << "; ";
    if (!cur->next_.contains(board[i][j])){
      return;
    }
    visited[i][j] = true;
    str.push_back(board[i][j]);
    if (cur->next_[board[i][j]]->end_){
      // cout << "emplace back " << i << ", " << j << "\n";
      res.insert(str);
      // trie_tree.remove(str);
    }
    vector<pair<int, int>> indices = {{i + 1, j}, {i - 1, j}, {i, j + 1}, {i, j - 1}};
    for (int idx = 0; idx < indices.size(); ++idx){
      // if (!cur->next_.contains(board[i][j])){
      //   continue;
      // }
      dfs(board, trie_tree, cur->next_[board[i][j]], indices[idx].first, indices[idx].second, visited, str, res);
    }
    str.pop_back();
    visited[i][j] = false;
  }
  vector<string> findWords(vector<vector<char>>& board, vector<string>& words) {
    // ios::sync_with_stdio(false); cin.tie(NULL);cout.tie(NULL);
    vector<string> res;
    int n = board.size();
    int m = board[0].size();
    int board_size = n * m;
    Trie trie_tree = Trie();
    for (const string& word : words){
      if (word.size() > board_size){
        continue;
      }
      trie_tree.insert(word);
    }
    string str;
    vector<vector<bool>> visited = vector(n, vector(m, false));
    for (int i = 0; i < n; ++i){
      for (int j = 0; j < m; ++j){
        // cout << i << ", " << j << "\n";
        unordered_set<string> temp;
        dfs(board, trie_tree, trie_tree.root_, i, j, visited, str, temp);
        for (const string& word : temp){
          trie_tree.remove(word);
          res.emplace_back(word);
        }
      }
    }
    return res;
  }
};
76723