Q2
unknown
c_cpp
2 years ago
1.7 kB
37
Indexable
bool isLeapYear(int year) {
return (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0);
}
bool isDateValid(string date) {
stringstream ss(date);
int year, month, day;
char delimiter;
ss >> year >> delimiter >> month >> delimiter >> day;
if (ss.fail() || delimiter != '-' || year < 0 || month < 1 || month > 12 || day < 1) {
return false;
}
int daysInMonth = 31;
if (month == 4 || month == 6 || month == 9 || month == 11) {
daysInMonth = 30;
} else if (month == 2) {
if (isLeapYear(year)) {
daysInMonth = 29;
} else {
daysInMonth = 28;
}
}
return day <= daysInMonth;
}
bool isTimeValid(string time) {
stringstream ss(time);
int hours, minutes, seconds;
char delimiter;
ss >> hours >> delimiter >> minutes >> delimiter >> seconds;
if (ss.fail() || delimiter != ':' || hours < 0 || hours >= 24 || minutes < 0 || minutes >= 60 || seconds < 0 || seconds >= 60) {
return false;
} else {
return true;
}
}
vector<vector<string>> countUserLogins(vector<vector<string>> logs){
map<string, map<string, int>> logins;
for(auto log: logs){
string user = log[0];
string time = log[1];
string date = log[2];
if(isDateValid(date) && isTimeValid(time)){
logins[user][date]++;
}
}
vector<vector<string>> res;
for(auto [user, dateMap]: logins){
for(auto [date, count]: dateMap){
res.push_back({user, date, to_string(count)});
}
}
return res;
}Editor is loading...