[InterviewBit] Reorder Data in Log Files

Reorder Data in Log Files

  • Time :
  • Space :
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
struct Log {
bool digitLog;
string identifier;
string log;
};

Log parse(string s) {
string id = "";
string log = "";
bool fl = false;
bool digit = true;
for(int i = 0; i < s.length(); i++) {
if(!fl and s[i] == '-') fl = true;
if(!fl) id.push_back(s[i]);
else {
if(s[i] != '-' and !isdigit(s[i])) digit = false;
log.push_back(s[i]);
}
}
return {digit, id, log};
}

vector<string> Solution::reorderLogs(vector<string> &A) {
vector<Log> letter, digit;
for(auto a : A) {
auto log = parse(a);
if(log.digitLog) digit.push_back(log);
else letter.push_back(log);
}
sort(begin(letter), end(letter), [](auto& a, auto& b) {
return a.log < b.log;
});
vector<string> res;
for(int i = 0; i < letter.size(); i++) {
res.push_back(letter[i].identifier + letter[i].log);
}
for(int i = 0; i < digit.size(); i++) {
res.push_back(digit[i].identifier + digit[i].log);
}
return res;
}
Author: Song Hayoung
Link: https://songhayoung.github.io/2022/11/10/PS/interviewbit/reorder-data-in-log-files/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.