-
Notifications
You must be signed in to change notification settings - Fork 0
/
log_files.cc
48 lines (45 loc) · 1.66 KB
/
log_files.cc
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
class Solution {
public:
vector<string> reorderLogFiles(vector<string> &logs) {
std::multimap<string, string> map;
std::vector<string> letter;
std::vector<string> digit;
std::vector<string> dup;
std::vector<string> output;
for (auto l : logs) {
auto pos = l.find(" ");
if (l.at((pos + 1)) < 'a') {
digit.push_back(l);
} else {
string first(l.begin() + pos + 1, l.end());
string second(l.begin(), l.begin() + pos);
letter.push_back(first);
map.emplace(first, second);
}
}
sort(letter.begin(), letter.end());
for (auto iter = letter.begin(); iter != letter.end(); iter++) {
// Traverse through each of the letter logs
auto l = *iter;
if (map.count(l) > 1) {
// Have multiple keys with same string
auto range = map.equal_range(l);
for (auto it = range.first; it != range.second; it++) {
string d = it->second + " " + it->first;
dup.push_back(d);
map.erase(it);
}
sort(dup.begin(), dup.end());
output.insert(output.end(), dup.begin(), dup.end());
dup.clear();
letter.erase(iter);
} else {
auto l_log = map.find(l);
string let = l_log->second + " " + l_log->first;
output.push_back(let);
}
}
output.insert(output.end(), digit.begin(), digit.end());
return output;
}
};