forked from shuboc/LeetCode-2
-
Notifications
You must be signed in to change notification settings - Fork 1
/
word-search-ii.cpp
102 lines (87 loc) · 2.86 KB
/
word-search-ii.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
// Time: O(m * n * h), h is height of trie
// Space: O(26^h)
class Solution {
private:
struct TrieNode {
bool isString = false;
unordered_map<char, TrieNode *> leaves;
bool Insert(const string& s) {
auto* p = this;
for (const auto& c : s) {
if (p->leaves.find(c) == p->leaves.cend()) {
p->leaves[c] = new TrieNode;
}
p = p->leaves[c];
}
// s already existed in this trie.
if (p->isString) {
return false;
} else {
p->isString = true;
return true;
}
}
~TrieNode() {
for (auto& kv : leaves) {
if (kv.second) {
delete kv.second;
}
}
}
};
public:
/**
* @param board: A list of lists of character
* @param words: A list of string
* @return: A list of string
*/
vector<string> findWords(vector<vector<char>>& board, vector<string>& words) {
unordered_set<string> ret;
vector<vector<bool>> visited(board.size(), vector<bool>(board[0].size(), false));
string cur;
TrieNode trie;
for (const auto& word : words) {
trie.Insert(word);
}
for (int i = 0; i < board.size(); ++i) {
for (int j = 0; j < board[0].size(); ++j) {
findWordsDFS(board, visited, &trie, i, j, cur, ret);
}
}
return vector<string>(ret.begin(), ret.end());
}
void findWordsDFS(vector<vector<char>> &grid,
vector<vector<bool>> &visited,
TrieNode *trie,
int i,
int j,
string cur,
unordered_set<string> &ret) {
// Invalid state.
if (!trie || i < 0 || i >= grid.size() || j < 0 || j >= grid[0].size()) {
return;
}
// Not in trie or visited.
if (!trie->leaves[grid[i][j] ] || visited[i][j]) {
return;
}
// Get next trie nodes.
TrieNode *nextNode = trie->leaves[grid[i][j]];
// Update current string.
cur.push_back(grid[i][j]);
// Find the string, add to the answers.
if (nextNode->isString) {
ret.insert(cur);
}
// Marked as visited.
visited[i][j] = true;
// Try each direction.
const vector<pair<int, int>> directions{{0, -1}, {0, 1},
{-1, 0}, {1, 0}};
for (const auto& d : directions) {
findWordsDFS(grid, visited, nextNode,
i + d.first, j + d.second, cur, ret);
}
visited[i][j] = false;
}
};