-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathsoln.cpp
62 lines (59 loc) · 1.97 KB
/
soln.cpp
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
54
55
56
57
58
59
60
61
62
struct TrieNode {
bool is_word;
vector<TrieNode *> children;
TrieNode(): is_word(false), children(26, nullptr) {}
~TrieNode() {
const int n = children.size();
for(int i = 0; i < n; ++i) {
if (children[i] != nullptr) {
delete children[i];
children[i] = nullptr;
}
}
}
};
class Solution {
public:
vector<string> findWords(vector<vector<char>>& board, vector<string>& words) {
vector<string> ans;
if (board.empty() || board[0].empty()) return ans;
TrieNode root = TrieNode();
for(const string & word : words) {
TrieNode * cur = & root;
for(char ch : word) {
if (cur->children[ch - 'a'] == nullptr) cur->children[ch - 'a'] = new TrieNode();
cur = cur->children[ch - 'a'];
}
cur->is_word = true;
}
int m = board.size(), n = board[0].size();
for(int i = 0; i < m; ++i) {
for(int j = 0; j < n; ++j) {
TrieNode * cur = & root;
string path;
dfs(i, j, board, ans, cur, path);
}
}
return ans;
}
private:
void dfs(int i, int j, vector<vector<char>> & board, vector<string> & ans, TrieNode * cur, string & path) {
if (i < 0 || i >= board.size() || j < 0 || j >= board[0].size() || board[i][j] == '#') return;
char ch = board[i][j];
board[i][j] = '#';
if (cur->children[ch - 'a'] != nullptr) {
cur = cur->children[ch - 'a'];
path += ch;
if (cur->is_word) {
ans.push_back(path);
cur->is_word = false;
}
dfs(i - 1, j, board, ans, cur, path);
dfs(i + 1, j, board, ans, cur, path);
dfs(i, j - 1, board, ans, cur, path);
dfs(i, j + 1, board, ans, cur, path);
path.pop_back();
}
board[i][j] = ch;
}
};