forked from kamyu104/LeetCode-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 14
/
before-and-after-puzzle.cpp
34 lines (33 loc) · 1.32 KB
/
before-and-after-puzzle.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
// Time: O(l * rlogr) , l is the max length of phrases
// , r is the number of result, could be up to O(n^2)
// Space: O(l * (n + r)), n is the number of phrases
class Solution {
public:
vector<string> beforeAndAfterPuzzles(vector<string>& phrases) {
unordered_map<string, vector<size_t>> lookup;
for (int i = 0; i < phrases.size(); ++i) {
const auto& phrase = phrases[i];
const auto& right = phrase.rfind(' ');
const auto& word = (right == string::npos) ? phrase : phrase.substr(right + 1);
lookup[word].emplace_back(i);
}
unordered_set<string> result_set;
for (int i = 0; i < phrases.size(); ++i) {
const auto& phrase = phrases[i];
const auto& left = phrase.find(' ');
const auto& word = (left == string::npos) ? phrase : phrase.substr(0, left);
if (!lookup.count(word)) {
continue;
}
for (const auto& j : lookup[word]) {
if (j == i) {
continue;
}
result_set.emplace(phrases[j] + phrase.substr(word.length()));
}
}
vector<string> result(result_set.cbegin(), result_set.cend());
sort(result.begin(), result.end());
return result;
}
};