LeetCode Problem Workspace
Reformat Date
Reformat Date requires transforming a date string from human-readable form to YYYY-MM-DD using a string-driven strategy efficiently.
1
Topics
6
Code langs
3
Related
Practice Focus
Easy · String-driven solution strategy
Answer-first summary
Reformat Date requires transforming a date string from human-readable form to YYYY-MM-DD using a string-driven strategy efficiently.
Ace coding interviews with Interview AiBoxInterview AiBox guidance for String-driven solution strategy
To solve Reformat Date, start by splitting the input into day, month, and year components, handling suffixes in the day carefully. Map the month abbreviations to numeric values and pad single-digit days or months with zeros. This string-driven approach ensures correctness and simplicity, avoiding unnecessary date libraries while achieving the required format conversion reliably.
Problem Statement
You are given a date string in the format "Day Month Year" where Day is 1st to 31st with suffixes, Month is a three-letter abbreviation, and Year is four digits. Your task is to convert this string into the format YYYY-MM-DD, standardizing day and month with leading zeros where needed.
For example, given date = "20th Oct 2052", the output should be "2052-10-20". Similarly, date = "6th Jun 1933" should return "1933-06-06". All input dates are valid and do not require error handling.
Examples
Example 1
Input: date = "20th Oct 2052"
Output: "2052-10-20"
Example details omitted.
Example 2
Input: date = "6th Jun 1933"
Output: "1933-06-06"
Example details omitted.
Example 3
Input: date = "26th May 1960"
Output: "1960-05-26"
Example details omitted.
Constraints
- The given dates are guaranteed to be valid, so no error handling is necessary.
Solution Approach
Split and Normalize Components
Separate the input string into day, month, and year. Remove ordinal suffixes like 'st', 'nd', 'rd', 'th' from the day and ensure it is two digits by padding with zero if necessary.
Map Month Abbreviations to Numbers
Use a dictionary mapping from 'Jan' to '01', 'Feb' to '02', and so on, to convert the three-letter month to its numeric representation, handling all twelve months accurately.
Combine into Standard Format
Concatenate the year, month, and normalized day into the final YYYY-MM-DD string. Return this string directly, ensuring consistent formatting for all inputs.
Complexity Analysis
| Metric | Value |
|---|---|
| Time | Depends on the final approach |
| Space | Depends on the final approach |
Time complexity is O(1) because the input size is fixed and splitting plus mapping operations are constant. Space complexity is O(1) as only a few variables and a fixed-size dictionary are used.
What Interviewers Usually Probe
- Look for explicit handling of day suffixes and zero-padding in the solution.
- Verify that month abbreviation mapping covers all months and handles case consistently.
- Expect a simple string-based solution without relying on built-in date libraries.
Common Pitfalls or Variants
Common pitfalls
- Forgetting to remove day suffixes before padding, leading to incorrect day formatting.
- Incorrect mapping of month abbreviations or typos in the dictionary.
- Failing to pad single-digit months or days, resulting in invalid YYYY-MM-DD format.
Follow-up variants
- Input dates could include varying day suffixes or whitespace requiring careful trimming.
- Month names could appear in full form rather than three-letter abbreviations.
- Extensions could involve converting multiple date strings in a list efficiently.
FAQ
What is the recommended approach to remove day suffixes in Reformat Date?
Split the day from the string and strip suffixes like 'st', 'nd', 'rd', 'th' before padding it to two digits.
How do I convert month abbreviations to numbers efficiently?
Use a fixed dictionary mapping each three-letter month abbreviation to its corresponding two-digit number.
Is it necessary to handle invalid dates in this problem?
No, all input dates are guaranteed valid, so no additional error checking is required.
Can I solve this without using any date libraries?
Yes, a string-driven approach is sufficient and preferred to convert day, month, and year into the correct format.
Why is zero-padding important in Reformat Date?
Zero-padding ensures single-digit days and months comply with the YYYY-MM-DD standard, preventing format errors.
Solution
Solution 1
#### Python3
class Solution:
def reformatDate(self, date: str) -> str:
s = date.split()
s.reverse()
months = " JanFebMarAprMayJunJulAugSepOctNovDec"
s[1] = str(months.index(s[1]) // 3 + 1).zfill(2)
s[2] = s[2][:-2].zfill(2)
return "-".join(s)Continue Topic
string
Practice more edge cases under the same topic.
arrow_forwardauto_awesomeContinue Pattern
String-driven solution strategy
Expand the same solving frame across more problems.
arrow_forwardsignal_cellular_altSame Difficulty Track
Easy
Stay on this level to stabilize interview delivery.
arrow_forward