Reformat Date - Complete Solution Guide
Reformat Date is LeetCode problem 1507, a Easy level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.
Problem Statement
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
Detailed Explanation
The problem requires reformatting a date string from a given format ("Day Month Year") to the standard ISO 8601 format ("YYYY-MM-DD"). The input date string adheres to a specific pattern: the day is represented with an ordinal suffix (e.g., "1st", "2nd", "3rd"), the month is a three-letter abbreviation (e.g., "Jan", "Feb", "Mar"), and the year is a four-digit number. The task involves extracting these components, converting them to the desired format, and concatenating them to produce the output string.
Solution Approach
All provided solutions follow a similar approach. They first split the input string into its day, month, and year components. Then, they use a lookup mechanism (dictionary, hashmap, or switch statement) to convert the month abbreviation to its numerical equivalent. Finally, they format the day and month to two digits using zero-padding and concatenate the year, month, and day to produce the output string in "YYYY-MM-DD" format.
Step-by-Step Algorithm
- Split the input date string into three parts: day, month, and year.
- Extract the numerical day value from the day part by removing the ordinal suffix (e.g., remove "th", "st", "nd", "rd").
- Convert the three-letter month abbreviation to its two-digit numerical representation using a pre-defined mapping (dictionary, hashmap, or switch statement).
- Zero-pad the day and month values to ensure they have two digits each.
- Concatenate the year, month, and day parts with hyphens to form the final "YYYY-MM-DD" formatted date string.
- Return the formatted date string.
Key Insights
- The input date string follows a consistent format, allowing for straightforward string manipulation techniques (splitting, substring extraction) to separate the day, month, and year components.
- A mapping (e.g., a dictionary/hash map/switch statement) is needed to translate the three-letter month abbreviations to their corresponding two-digit numerical representations.
- Zero-padding is necessary for both the day and month components to ensure they are always represented with two digits (e.g., "01" instead of "1").
Complexity Analysis
Time Complexity: O(1)
Space Complexity: O(1)
Topics
This problem involves: String.
Companies
Asked at: Celigo, Expedia, Twilio, Veritas.