-
Notifications
You must be signed in to change notification settings - Fork 0
/
212. Word Search II.cpp
68 lines (68 loc) · 1.55 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
#include<vector>
using namespace std;
#define MAX 26
class TrieNode {
public:
TrieNode *next[MAX];
bool isWord;
TrieNode() :isWord(false) {
memset(next, NULL, sizeof(next));
}
};
class TrieFunc {
TrieNode *root;
public:
TrieFunc() {
root = new TrieNode();
}
TrieNode *GetRoot() {
return root;
}
void insert(string word) {
TrieNode *p = root;
for (auto &a : word) {
if (!p->next[a - 'a'])
p->next[a - 'a'] = new TrieNode();
p = p->next[a - 'a'];
}
p->isWord = true;
}
};
class Solution {
vector<string> ret;
public:
void searchWord(vector<vector<char>>& board, string curr,TrieNode *root,int i,int j) {
int m = board.size();
int n = board[0].size();
if (i < 0 || i >= m || j < 0 || j >= n || !root) return ;
if (!root->next[board[i][j] - 'a']) return ;
root = root->next[board[i][j] - 'a'];
char temp = board[i][j];
board[i][j] = '\0';
curr.push_back(temp);
if (root->isWord) {
root->isWord = false;
ret.push_back(curr);
}
searchWord(board, curr, root, i + 1, j);
searchWord(board, curr, root, i - 1, j);
searchWord(board, curr, root, i, j + 1);
searchWord(board, curr, root, i, j - 1);
board[i][j] = temp;
curr.pop_back();
}
vector<string> findWords(vector<vector<char>>& board, vector<string>& words) {
string curr;
if (board.empty()) return ret;
TrieFunc trie;
for (auto &a : words) {
trie.insert(a);
}
TrieNode *root = trie.GetRoot();
for (int i = 0; i < board.size(); i++)
for (int j = 0; j < board[0].size(); j++) {
searchWord(board, curr, root, i, j);
}
return ret;
}
};