-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy path634.word-squares.cpp
240 lines (212 loc) · 5.92 KB
/
634.word-squares.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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
// Tag: Depth First Search/DFS, Trie
// Time: O(N * P^5)
// Space: O(N)
// Ref: Leetcode-425
// Note: -
// Given a set of words **without duplicates**, find all [`word squares`](https://en.wikipedia.org/wiki/Word_square "Word square") you can build from them.
//
// A sequence of words forms a valid word square if the kth row and column read the exact same string, where 0 ≤ k < max(numRows, numColumns).
//
// For example, the word sequence `["ball","area","lead","lady"]` forms a word square because each word reads the same both horizontally and vertically.
// ```
// b a l l
// a r e a
// l e a d
// l a d y
// ```
//
// **Example 1:**
// ```
// Input:
// ["area","lead","wall","lady","ball"]
// Output:
// [["wall","area","lead","lady"],["ball","area","lead","lady"]]
//
// Explanation:
// The output consists of two word squares. The order of output does not matter (just the order of words in each word square matters).
// ```
//
// **Example 2:**
// ```
// Input:
// ["abat","baba","atan","atal"]
// Output:
// [["baba","abat","baba","atan"],["baba","abat","baba","atal"]]
// ```
//
// - There are at least 1 and at most 1000 words.
// - All words will have the exact same length.
// - Word length is at least 1 and at most 5.
// - Each word contains only lowercase English alphabet `a-z`.
class TrieNode {
public:
vector<TrieNode *> children;
bool is_word;
vector<string> prefix;
TrieNode(): children(26, nullptr), is_word(false), prefix() {}
~TrieNode() {
for (auto p: children) {
delete p;
}
}
};
class Trie {
public:
TrieNode *root;
Trie() {
root = new TrieNode();
}
~Trie() {
delete root;
}
void add(string word) {
TrieNode *cur = root;
for (auto ch: word) {
if (!cur->children[ch - 'a']) {
cur->children[ch - 'a'] = new TrieNode();
}
cur = cur->children[ch - 'a'];
cur->prefix.push_back(word);
}
cur->is_word = true;
}
vector<string> prefix(string word) {
TrieNode *cur = root;
for (auto ch: word) {
if (!cur->children[ch - 'a']) {
return vector<string>{};
}
cur = cur->children[ch - 'a'];
}
return cur->prefix;
};
};
class Solution {
public:
/**
* @param words: a set of words without duplicates
* @return: all word squares
* we will sort your return value in output
*/
vector<vector<string>> wordSquares(vector<string> &words) {
// write your code here
int n = words[0].size();
Trie tree = Trie();
for (auto word: words){
tree.add(word);
}
vector<vector<string>> res;
for (auto word: words) {
vector<string> ans = vector<string>{word};
helper(n, tree, ans, res);
}
return res;
}
void helper(int n, Trie &tree, vector<string> &ans, vector<vector<string>> &res) {
if (ans.size() == n) {
res.push_back(ans);
return;
}
int k = ans.size();
string pre = "";
for (int i = 0; i < k; i++) {
pre += ans[i][k];
}
for (auto word: tree.prefix(pre)) {
ans.push_back(word);
helper(n, tree, ans, res);
ans.pop_back();
}
}
};
class TrieNode {
public:
vector<TrieNode *> children;
bool is_word;
string word;
TrieNode(): children(26, nullptr), is_word(false), word() {}
~TrieNode() {
for (auto p: children) {
delete p;
}
}
};
class Trie {
public:
TrieNode *root;
Trie() {
root = new TrieNode();
}
~Trie() {
delete root;
}
void add(string word) {
TrieNode *cur = root;
for (auto ch: word) {
if (!cur->children[ch - 'a']) {
cur->children[ch - 'a'] = new TrieNode();
}
cur = cur->children[ch - 'a'];
}
cur->is_word = true;
cur->word = word;
}
vector<string> prefix(string word) {
TrieNode *cur = root;
for (auto ch: word) {
if (!cur->children[ch - 'a']) {
return vector<string>{};
}
cur = cur->children[ch - 'a'];
}
vector<string> res;
dfs(res, cur);
return res;
};
void dfs(vector<string> &res, TrieNode *cur) {
if (cur->is_word) {
res.push_back(cur->word);
}
for (auto &neighbor: cur->children) {
if (neighbor) {
dfs(res, neighbor);
}
}
}
};
class Solution {
public:
/**
* @param words: a set of words without duplicates
* @return: all word squares
* we will sort your return value in output
*/
vector<vector<string>> wordSquares(vector<string> &words) {
// write your code here
int n = words[0].size();
Trie tree = Trie();
for (auto word: words){
tree.add(word);
}
vector<vector<string>> res;
vector<string> ans;
helper(n, tree, ans, res);
return res;
}
void helper(int n, Trie &tree, vector<string> &ans, vector<vector<string>> &res) {
if (ans.size() == n) {
res.push_back(ans);
return;
}
int k = ans.size();
string pre = "";
for (int i = 0; i < k; i++) {
pre += ans[i][k];
}
for (auto word: tree.prefix(pre)) {
ans.push_back(word);
helper(n, tree, ans, res);
ans.pop_back();
}
}
};