forked from RajwardhanShinde/LeetCode-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathWordSearchII.cpp
61 lines (57 loc) · 1.79 KB
/
WordSearchII.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
struct TrieNode {
bool end;
TrieNode *children[26];
TrieNode() {
end = false;
memset(children, NULL, sizeof(children));
}
};
class Trie {
public:
TrieNode* root;
Trie(vector<string>& words) {
root = new TrieNode();
for(int i = 0; i < words.size(); i++)
addWord(words[i]);
}
void addWord(const string& s) {
TrieNode *dummy = root;
for(char c: s) {
if(!dummy->children[c - 'a'])
dummy->children[c - 'a'] = new TrieNode();
dummy = dummy->children[c - 'a'];
}
dummy->end = true;
}
};
class Solution {
public:
vector<string> findWords(vector<vector<char>>& board, vector<string>& words) {
Trie* trie = new Trie(words);
set<string> s;
TrieNode *root = trie->root;
for(int x = 0; x < board.size(); x++) {
for(int y = 0; y < board[0].size(); y++) {
findWords(board, x, y, root, "", s);
}
}
vector<string> res;
for(auto it: s) res.push_back(it);
return res;
}
void findWords(vector<vector<char>>& board, int x, int y, TrieNode *root, string word, set<string>& s) {
if(x < 0 || x >= board.size() || y < 0 || y >= board[0].size() || board[x][y] == ' ') return;
if(root->children[board[x][y] - 'a']) {
word += board[x][y];
root = root->children[board[x][y] - 'a'];
if(root->end) s.insert(word);
char c = board[x][y];
board[x][y] = ' ';
findWords(board, x+1, y, root, word, s);
findWords(board, x-1, y, root, word, s);
findWords(board, x, y+1, root, word, s);
findWords(board, x, y-1, root, word, s);
board[x][y] = c;
}
}
};