forked from liuyubobobo/Play-Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
137 lines (106 loc) · 3.23 KB
/
main.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
/// Source : https://leetcode.com/problems/word-search-ii/
/// Author : liuyubobobo
/// Time : 2019-02-08
#include <iostream>
#include <vector>
#include <unordered_map>
#include <unordered_set>
using namespace std;
/// Trie + DFS
/// The Trie class is implemented in 208
///
/// Time Complexity: O(4 ^ (m * n) * maxlen)
/// Space Complexity: O(m * n + total_letters_in_words)
class Trie{
private:
struct Node{
unordered_map<char, int> next;
bool end = false;
};
vector<Node> trie;
public:
Trie(){
trie.clear();
trie.push_back(Node());
}
/** Inserts a word into the trie. */
void insert(const string& word){
int treeID = 0;
for(char c: word){
if(trie[treeID].next.find(c) == trie[treeID].next.end()){
trie[treeID].next[c] = trie.size();
trie.push_back(Node());
}
treeID = trie[treeID].next[c];
}
trie[treeID].end = true;
}
/** Returns if the word is in the trie. */
bool search(const string& word){
int treeID = 0;
for(char c: word){
if(trie[treeID].next.find(c) == trie[treeID].next.end())
return false;
treeID = trie[treeID].next[c];
}
return trie[treeID].end;
}
/** Returns if there is any word in the trie that starts with the given prefix. */
bool startsWith(const string& prefix) {
int treeID = 0;
for(char c: prefix){
if(trie[treeID].next.find(c) == trie[treeID].next.end())
return false;
treeID = trie[treeID].next[c];
}
return true;
}
};
class Solution {
private:
int d[4][2] = {{-1, 0}, {0,1}, {1, 0}, {0, -1}};
int m, n;
int maxlen = 0;
public:
vector<string> findWords(vector<vector<char>>& board, vector<string>& words) {
Trie trie;
for(const string& word: words){
trie.insert(word);
maxlen = max(maxlen, (int)word.size());
}
unordered_set<string> res;
m = board.size();
assert( m > 0 );
n = board[0].size();
for(int i = 0 ; i < m ; i ++)
for(int j = 0 ; j < n ; j ++){
vector<vector<bool>> visited(m, vector<bool>(n, false));
searchWord(board, trie, i, j, "", visited, res);
}
return vector<string>(res.begin(), res.end());
}
private:
// start from board[x][y], find word s
void searchWord(const vector<vector<char>> &board, Trie& trie,
int x, int y, string s,
vector<vector<bool>>& visited, unordered_set<string>& res){
s += board[x][y];
if(s.size() > maxlen) return;
if(!trie.startsWith(s)) return;
if(trie.search(s)) res.insert(s);
visited[x][y] = true;
for(int i = 0 ; i < 4 ; i ++){
int nextx = x + d[i][0];
int nexty = y + d[i][1];
if(inArea(nextx, nexty) && !visited[nextx][nexty])
searchWord(board, trie, nextx, nexty, s, visited, res);
}
visited[x][y] = false;
}
bool inArea(int x , int y){
return x >= 0 && x < m && y >= 0 && y < n;
}
};
int main() {
return 0;
}