forked from kamyu104/LeetCode-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreformat-date.cpp
More file actions
22 lines (20 loc) · 734 Bytes
/
reformat-date.cpp
File metadata and controls
22 lines (20 loc) · 734 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
// Time: O(n)
// Space: O(1)
class Solution {
public:
string reformatDate(string date) {
static const unordered_map<string, int> lookup = {
{"Jan", 1}, {"Feb", 2}, {"Mar", 3}, {"Apr", 4},
{"May", 5}, {"Jun", 6}, {"Jul", 7}, {"Aug", 8},
{"Sep", 9}, {"Oct", 10}, {"Nov", 11}, {"Dec", 12}
};
stringstream ss;
int y = stoi(date.substr(date.length() - 4));
ss << setfill('0') << setw(4) << y;
int m = lookup.at(date.substr(date.length() - 4 - 4, 3));
ss << "-" << setfill('0') << setw(2) << m;
int d = stoi(date.substr(0, date.find(' ', 0) - 2));
ss << "-" << setfill('0') << setw(2) << d;
return ss.str();
}
};