Watch 10 video solutions for Reformat Date, a easy level problem involving String. This walkthrough by Naresh Gupta has 1,314 views views. Want to try solving it yourself? Practice on FleetCode or read the detailed text solution.
Given a date string in the form Day Month Year, where:
Day is in the set {"1st", "2nd", "3rd", "4th", ..., "30th", "31st"}.Month is in the set {"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"}.Year is in the range [1900, 2100].Convert the date string to the format YYYY-MM-DD, where:
YYYY denotes the 4 digit year.MM denotes the 2 digit month.DD denotes the 2 digit day.
Example 1:
Input: date = "20th Oct 2052" Output: "2052-10-20"
Example 2:
Input: date = "6th Jun 1933" Output: "1933-06-06"
Example 3:
Input: date = "26th May 1960" Output: "1960-05-26"
Constraints:
Problem Overview: You receive a human-readable date string like "20th Oct 2052". The goal is to convert it into ISO format "2052-10-20". The challenge is extracting the day while removing ordinal suffixes (st, nd, rd, th) and converting the month abbreviation into its numeric representation.
Approach 1: String Parsing and Dictionary Mapping (Time: O(n), Space: O(1))
The most direct solution splits the input string into three parts: day, month, and year. The day portion contains a numeric value followed by a suffix (for example "6th" or "21st"), so you extract the numeric characters and pad with a leading zero if needed. The month abbreviation (Jan, Feb, etc.) is converted using a fixed dictionary that maps each month to its two-digit number. The year already appears in the required format. Finally, concatenate the parts as year-month-day. This approach relies only on basic string operations such as splitting, substring extraction, and dictionary lookup.
The key insight is that the month set is small and fixed (12 values), so a constant-time mapping works best. Each part of the string is processed once, giving linear time relative to the input length. Because the dictionary size never changes, the space usage is constant.
Approach 2: Regular Expression and String Manipulation (Time: O(n), Space: O(n))
Another option uses regular expressions to extract structured components from the input. A regex pattern can capture the numeric day, ignore the ordinal suffix, and isolate the month abbreviation and year. After matching, convert the month abbreviation through the same lookup table used in the previous method. Format the output string using zero-padded day and month values.
This method is useful when input formats vary slightly or when you want cleaner extraction logic in languages with strong regex support. However, regex introduces extra overhead and temporary match objects, which increases memory usage compared to simple splitting. The time complexity remains linear because the pattern scans the input string once.
Recommended for interviews: The string parsing and dictionary mapping approach is the expected solution. It shows that you can reason about input structure and build a straightforward transformation with constant auxiliary memory. Regex solutions work, but interviewers typically prefer explicit parsing since it demonstrates stronger control over string manipulation and avoids unnecessary abstraction. If you explain both options, start with manual parsing to show clarity, then mention regex as an alternative implementation.
| Approach | Time | Space | When to Use |
|---|---|---|---|
| String Parsing and Dictionary Mapping | O(n) | O(1) | Best general solution. Clear logic, constant memory, and easy to implement in interviews. |
| Regular Expression and String Manipulation | O(n) | O(n) | Useful when parsing structured text or when regex utilities simplify extraction. |