Problem Description
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.
Key Insights
- The input date string needs to be parsed to extract the day, month, and year.
- The day string may contain ordinal suffixes ("st", "nd", "rd", "th") that need to be removed to convert it to a numerical format.
- The month string must be mapped to its corresponding numerical representation.
- The output format requires leading zeros for the day and month if they are single digits.
Space and Time Complexity
Time Complexity: O(1) - The operations performed to format the date do not depend on the size of the input data. Space Complexity: O(1) - The space used is constant, as it only stores a fixed number of variables.
Solution
To solve the problem, we can use the following steps:
- Split the input date string into its components (day, month, year).
- Remove the ordinal suffix from the day and convert it to an integer.
- Map the month from its string representation to a numeric value.
- Format the year, month, and day into the output string in the specified format.