-
-
Notifications
You must be signed in to change notification settings - Fork 8.8k
/
Copy pathSolution.cpp
27 lines (27 loc) · 910 Bytes
/
Solution.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
class Solution {
public:
vector<string> beforeAndAfterPuzzles(vector<string>& phrases) {
int n = phrases.size();
pair<string, string> ps[n];
for (int i = 0; i < n; ++i) {
int j = phrases[i].find(' ');
if (j == string::npos) {
ps[i] = {phrases[i], phrases[i]};
} else {
int k = phrases[i].rfind(' ');
ps[i] = {phrases[i].substr(0, j), phrases[i].substr(k + 1)};
}
}
unordered_set<string> s;
for (int i = 0; i < n; ++i) {
for (int j = 0; j < n; ++j) {
if (i != j && ps[i].second == ps[j].first) {
s.insert(phrases[i] + phrases[j].substr(ps[i].second.size()));
}
}
}
vector<string> ans(s.begin(), s.end());
sort(ans.begin(), ans.end());
return ans;
}
};