Link: 1507. Reformat Date - Easy
Question
Restate the problem
We’re given a date string in the format “Day Month Year”, like "20th Oct 2052".
We need to convert it to the standardized numeric format “YYYY-MM-DD”, where the month is a two-digit number and the day is two digits (with leading zeros if needed).
Method 1
Method 2 - Hashmap + split + slicing
Method
Approach
- For this problem, I first split the input string into three parts: day, month, and year.
- For the day, I remove the suffix like st/nd/rd/th and keep only the digits, then zero-pad it to two digits.
- For the month, I use a hash map to convert the month abbreviation (e.g., Jan, Feb) into a two-digit number.
- The year stays the same.
- Finally, I concatenate them in the format
YYYY-MM-DD.
Complexity
- Time complexity: O(n)
- split: O(n)
- Space complexity: O(n)
Edge Case
Code
class Solution:
def reformatDate(self, date: str) -> str:
day, month, year = date.split(" ")
dic = {
"Jan": '01', "Feb": '02', "Mar": '03',
"Apr": '04', "May": '05', "Jun": '06',
"Jul": '07', "Aug": '08', "Sep": '09',
"Oct": '10', "Nov": '11', "Dec":'12'}
dd = day[:-2].zfill(2)
return year + "-" + dic[month] + "-" + ddHistory
Jan-24-2026 Solved
Jan-19-2026 hinted don’t know zfill