-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Expand file tree
/
Copy pathword-squares-ii.cpp
More file actions
65 lines (63 loc) · 2.21 KB
/
word-squares-ii.cpp
File metadata and controls
65 lines (63 loc) · 2.21 KB
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
// Time: O(n^4)
// Space: O(n)
// sort, brute force, hash table
class Solution {
public:
vector<vector<string>> wordSquares(vector<string>& words) {
ranges::sort(words);
unordered_map<int, vector<int>> lookup;
for (int i = 0; i < size(words); ++i) {
lookup[words[i][0] - 'a'].emplace_back(i);
lookup[(words[i][0] - 'a') + (words[i][3] - 'a' + 1) * 27].emplace_back(i);
}
vector<vector<string>> result;
for (int i = 0; i < size(words); ++i) {
for (const auto& j : lookup[words[i][0] - 'a']) {
if (j == i) {
continue;
}
for (const auto& k : lookup[words[i][3] - 'a']) {
if (k == i || k == j) {
continue;
}
for (const auto& l : lookup[(words[j][3] - 'a') + (words[k][3] - 'a' + 1) * 27]) {
if (l == i || l == j || l == k) {
continue;
}
result.push_back({words[i], words[j], words[k], words[l]});
}
}
}
}
return result;
}
};
// Time: O(n^4)
// Space: O(1)
// sort, brute force
class Solution2 {
public:
vector<vector<string>> wordSquares(vector<string>& words) {
ranges::sort(words);
vector<vector<string>> result;
for (int i = 0; i < size(words); ++i) {
for (int j = 0; j < size(words); ++j) {
if (j == i || words[j][0] != words[i][0]) {
continue;
}
for (int k = 0; k < size(words); ++k) {
if (k == i || k == j || words[k][0] != words[i][3]) {
continue;
}
for (int l = 0; l < size(words); ++l) {
if (l == i || l == j || l == k || words[l][0] != words[j][3] || words[l][3] != words[k][3]) {
continue;
}
result.push_back({words[i], words[j], words[k], words[l]});
}
}
}
}
return result;
}
};