Skip to content

Commit f4b912c

Browse files
authored
Create reformat-date.cpp
1 parent cb7163b commit f4b912c

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

C++/reformat-date.cpp

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// Time: O(n)
2+
// Space: O(1)
3+
4+
class Solution {
5+
public:
6+
string reformatDate(string date) {
7+
static const unordered_map<string, int> lookup = {
8+
{"Jan", 1}, {"Feb", 2}, {"Mar", 3}, {"Apr", 4},
9+
{"May", 5}, {"Jun", 6}, {"Jul", 7}, {"Aug", 8},
10+
{"Sep", 9}, {"Oct", 10}, {"Nov", 11}, {"Dec", 12}
11+
};
12+
stringstream ss;
13+
int y = stoi(date.substr(date.length() - 4));
14+
ss << setfill('0') << setw(4) << y;
15+
int m = lookup.at(date.substr(date.length() - 4 - 4, 3));
16+
ss << "-" << setfill('0') << setw(2) << m;
17+
int d = stoi(date.substr(0, date.find(' ', 0) - 2));
18+
ss << "-" << setfill('0') << setw(2) << d;
19+
return ss.str();
20+
}
21+
};
22+

0 commit comments

Comments
 (0)