-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path212. Word Search II.cpp
More file actions
53 lines (44 loc) · 1.34 KB
/
212. Word Search II.cpp
File metadata and controls
53 lines (44 loc) · 1.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
struct Trie {
Trie* child[26] = {};
string word = ""; // terminal string
};
class Solution {
public:
vector<string> ans;
ll m, n;
void insert(Trie* root, string& w) {
for (char c : w) {
if (!root->child[c - 'a']) root->child[c - 'a'] = new Trie();
root = root->child[c - 'a'];
}
root->word = w; // mark end of word
}
void dfs(vector<vector<char>>& b, ll i, ll j, Trie* node) {
char c = b[i][j];
if (c == '#' || !node->child[c - 'a']) return;
node = node->child[c - 'a'];
if (!node->word.empty()) {
ans.push_back(node->word);
node->word = ""; // avoid duplicates
}
b[i][j] = '#';
if (i > 0) dfs(b, i - 1, j, node);
if (j > 0) dfs(b, i, j - 1, node);
if (i < m - 1) dfs(b, i + 1, j, node);
if (j < n - 1) dfs(b, i, j + 1, node);
b[i][j] = c;
}
vector<string> findWords(vector<vector<char>>& board, vector<string>& words) {
Trie* root = new Trie();
for (auto& w : words) insert(root, w);
m = board.size();
n = board[0].size();
for (ll i = 0; i < m; i++)
for (ll j = 0; j < n; j++)
dfs(board, i, j, root);
return ans;
}
};