-
Notifications
You must be signed in to change notification settings - Fork 0
/
word-search-ii.cpp
57 lines (54 loc) · 1.69 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
class Trie{
public:
Trie* children[26];
bool endOfWord;
Trie():endOfWord(false){
for(int i = 0; i < 26; ++i)
children[i] = nullptr;
}
~Trie(){
for(int i = 0; i < 26; ++i)
if(children[i]) delete children[i];
}
void insert(string word){
Trie* curr = this;
for(char c: word){
if(!curr->children[c-'a']) curr->children[c - 'a'] = new Trie();
curr = curr->children[c-'a'];
}
curr->endOfWord = true;
}
};
class Solution {
void dfs(vector<vector<char>>& board, int i, int j, Trie* trie, unordered_set<string>& result, string s){
char c = board[i][j];
if(c == '$') return;
board[i][j] = '$';
Trie* t = trie->children[c - 'a'];
if(t){
string ss = s + c;
if(t->endOfWord) result.insert(ss);
if(i < board.size()-1) dfs(board, i+1, j, t, result, ss);
if(i > 0) dfs(board, i-1, j, t, result, ss);
if(j < board[0].size()-1) dfs(board, i, j+1, t, result, ss);
if(j > 0) dfs(board, i, j-1, t, result, ss);
}
board[i][j] = c;
}
public:
vector<string> findWords(vector<vector<char>>& board, vector<string>& words) {
if(words.size() == 0) return {};
Trie trie;
for(string w: words){
trie.insert(w);
}
unordered_set<string> result_s;
for(int i = 0; i < board.size(); ++i){
for(int j = 0; j < board[0].size(); ++j){
dfs(board, i, j, &trie, result_s, "");
}
}
vector<string> result(result_s.begin(), result_s.end());
return result;
}
};