-
Notifications
You must be signed in to change notification settings - Fork 0
/
212.word-search-ii.cpp
201 lines (198 loc) · 6.16 KB
/
212.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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
// version 1 : brute force + backtracking, TLE
/*
class Solution {
public:
vector<string> findWords(vector<vector<char>>& board, vector<string>& words) {
unordered_set<string> res;
for (int i = 0; i < words.size(); i++) {
if (exist(board, words[i])) {
res.insert(words[i]);
}
}
vector<string> resv;
for (auto s : res) {
resv.push_back(s);
}
return resv;
}
bool exist(vector<vector<char>>& board, string word) {
if (word.empty()) {
return true;
}
if (board.empty() || board[0].empty()) {
return false;
}
int m = board.size(), n = board[0].size();
vector<vector<bool>> visited(m, vector<bool>(n, false));
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
if (helper(board, visited, word, 0, i, j)) {
return true;
}
}
}
return false;
}
bool helper(vector<vector<char>> &board, vector<vector<bool>> &visited, string word, int start, int i, int j) {
if (start == word.size()) {
return true;
}
vector<pair<int, int>> dirs{{0, 1}, {0, -1}, {-1, 0}, {1, 0}};
int m = board.size(), n = board[0].size();
for (int d = 0; d < 4; d++) {
int ni = i + dirs[d].first, nj = j + dirs[d].second;
if (i >= 0 && j >= 0 && i < m && j < n && !visited[i][j] && board[i][j] == word[start]) {
visited[i][j] = true;
if (helper(board, visited, word, start + 1, ni, nj)) {
return true;
}
visited[i][j] = false;
}
}
return false;
}
};
*/
// version 2 : trie tree, store the words
/*
class Solution {
public:
struct TrieNode {
TrieNode* children[26];
string word;
TrieNode(): word("") {
for (auto &c : children) {
c = NULL;
}
}
};
struct Trie {
TrieNode *root;
Trie(): root(new TrieNode()) {}
void insert(string s) {
TrieNode *p = root;
for (auto c : s) {
int i = c - 'a';
if (!p->children[i]) {
p->children[i] = new TrieNode();
}
p = p->children[i];
}
p->word = s;
}
};
vector<string> findWords(vector<vector<char>>& board, vector<string>& words) {
vector<string> res;
if (words.empty() || board.empty()) {
return res;
}
int m = board.size(), n = board[0].size();
vector<vector<bool>> visited(m, vector<bool>(n, false));
Trie trie;
for (auto word : words) {
trie.insert(word);
}
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
helper(board, visited, res, i, j, trie.root);
}
}
return res;
}
void helper(vector<vector<char>> &board, vector<vector<bool>> &visited, vector<string> &res, int i, int j, TrieNode* p) {
if (!p->word.empty()) {
res.push_back(p->word);
p->word.clear();
}
vector<pair<int, int>> dirs{{0, 1}, {0, -1}, {1, 0}, {-1, 0}};
int m = board.size(), n = board[0].size();
for (auto dir : dirs) {
int ni = i + dir.first, nj = j + dir.second;
if (i >= 0 && i < m && j >= 0 && j < n && p->children[board[i][j] - 'a'] && !visited[i][j]) {
visited[i][j] = true;
helper(board, visited, res, ni, nj, p->children[board[i][j] - 'a']);
visited[i][j] = false;
}
}
}
};
*/
// version 3 : trie tree, store the board, TLE
class Solution {
public:
struct TrieNode {
TrieNode* children[26];
string word;
TrieNode(): word("") {
for (auto &c : children) {
c = NULL;
}
}
};
struct Trie {
TrieNode* root;
Trie(): root(new TrieNode()){};
void insert(string s) {
TrieNode *p = root;
for (auto c : s) {
int i = c - 'a';
if (!p->children[i]) {
p->children[i] = new TrieNode();
}
p = p->children[i];
}
p->word = s;
}
bool find(string s) {
TrieNode *p = root;
for(auto c : s) {
int i = c - 'a';
if (!p->children[i]) {
return false;
}
p = p->children[i];
}
if (p->word.empty()) {
return false;
}
p->word.clear();
return true;
}
};
vector<string> findWords(vector<vector<char>>& board, vector<string>& words) {
vector<string> res;
if (words.empty() || board.empty() || board[0].empty()) {
return res;
}
int m = board.size(), n = board[0].size();
Trie trie;
string cur = "";
vector<vector<bool>> visited(m, vector<bool>(n, false));
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
helper(board, visited, i, j, cur, trie);
}
}
for (auto word : words) {
if (trie.find(word)) {
res.push_back(word);
}
}
return res;
}
void helper(vector<vector<char>> &board, vector<vector<bool>> &visited, int i, int j, string &cur, Trie &trie) {
vector<pair<int, int>> dirs{{-1, 0}, {1, 0}, {0, 1}, {0, -1}};
int m = board.size(), n = board[0].size();
for (auto dir : dirs) {
int ni = i + dir.first, nj = j + dir.second;
if (i >= 0 && j >= 0 && i < m && j < n && !visited[i][j]) {
visited[i][j] = true;
cur += board[i][j];
trie.insert(cur);
helper(board, visited, ni, nj, cur, trie);
cur.pop_back();
visited[i][j] = false;
}
}
}
};