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.
In this approach, we will parse the given date string to extract the day, month, and year. We will then use a dictionary to map the month abbreviations to their respective numeric representations. Finally, the result is formatted to 'YYYY-MM-DD'.
This C solution involves splitting the input string into its components using sscanf, mapping the month to its numeric value, and then formatting it to the desired date format using sprintf. A fixed-size array is used for returning the formatted date.
Time Complexity: O(1), as we simply parse a fixed-length string and perform constant time operations.
Space Complexity: O(1), for storing result and month mapping.
Here, we utilize a regular expression to extract the day, month, and year separately. This approach eliminates multiple string operations by capturing groups when matching patterns directly.
This C solution uses regular expressions to match and extract parts directly from the date string, reducing manual parsing efforts.
Time Complexity: O(1), as regex processing and substring extraction are linear in relation to string length.
Space Complexity: O(1), with fixed allocation for regex and output.
| Approach | Complexity |
|---|---|
| String Parsing and Dictionary Mapping | Time Complexity: O(1), as we simply parse a fixed-length string and perform constant time operations. |
| Regular Expression and String Manipulation | Time Complexity: O(1), as regex processing and substring extraction are linear in relation to string length. |
| Default Approach | — |
| 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. |
reformat date | reformat date leetcode | leetcode 1507 • Naresh Gupta • 1,314 views views
Watch 9 more video solutions →Practice Reformat Date with our built-in code editor and test cases.
Practice on FleetCode