#1507
Reformat Date
EasyString
Approaches
💡
Intuition
Time Space
The brute force approach involves breaking down the date string into its components: day, month, and year. We then manually convert each component into the desired format, which can be straightforward but inefficient.
⚙️
Algorithm
4 steps- 1Step 1: Split the input date string into day, month, and year.
- 2Step 2: Remove the last two characters from the day to get the numeric value.
- 3Step 3: Map the month abbreviation to its corresponding numeric value.
- 4Step 4: Format the year, month, and day into 'YYYY-MM-DD'.
solution.py6 lines
1def reformatDate(date):
2 month_map = {'Jan': '01', 'Feb': '02', 'Mar': '03', 'Apr': '04', 'May': '05', 'Jun': '06', 'Jul': '07', 'Aug': '08', 'Sep': '09', 'Oct': '10', 'Nov': '11', 'Dec': '12'}
3 day, month, year = date.split()
4 day = day[:-2].zfill(2)
5 month = month_map[month]
6 return f'{year}-{month}-{day}'Solutions and explanations are original Tejav content. Problem titles © LeetCode — use the LeetCode button above for the full problem statement.