{x}
blog image

Reformat Date

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:

  • The given dates are guaranteed to be valid, so no error handling is necessary.

Solution Explanation for Reformat Date

This problem involves converting a date string from a specific format ("Day Month Year") to the standard "YYYY-MM-DD" format. The solution utilizes string manipulation and potentially lookup tables to achieve this efficiently.

Approach

The core idea is to split the input string into its day, month, and year components, then reformat each component according to the desired output format. The month requires a mapping from the abbreviated month name (e.g., "Jan", "Feb") to its numerical representation (e.g., "01", "02"). The day needs to be extracted while removing the suffix (e.g., "th", "rd"). Finally, the components are concatenated with hyphens to produce the result.

Code Explanation (Python)

The Python solution showcases a concise approach:

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)
  1. Splitting the String: s = date.split() splits the input string into a list of strings representing the day, month, and year. s.reverse() reverses the order to align with the desired "YYYY-MM-DD" sequence.

  2. Month Mapping: months = " JanFebMarAprMayJunJulAugSepOctNovDec" creates a string containing all month abbreviations. months.index(s[1]) // 3 + 1 finds the index of the month abbreviation in the months string, divides by 3 (since each month abbreviation takes 3 characters) and adds 1 to get the month number. zfill(2) pads the month number with a leading zero if necessary to ensure a two-digit representation.

  3. Day Extraction: s[2] = s[2][:-2].zfill(2) slices the day string to remove the last two characters (the suffix like "th" or "rd") and then pads it with a leading zero if necessary.

  4. Joining Components: return "-".join(s) joins the year, month, and day components with hyphens to form the final output string.

Time and Space Complexity Analysis

  • Time Complexity: O(1). The operations performed (splitting, indexing, slicing, joining) all take constant time because the input string length is limited.

  • Space Complexity: O(1). The auxiliary space used is constant, regardless of the input string's content. The space used by variables like months, s, etc., is fixed.

Other Language Solutions

The solutions in other languages (Java, C++, Go, TypeScript, PHP) follow a similar logic, adapting the syntax to the specific language's features. They typically use string manipulation functions for splitting, indexing, and formatting, and may utilize different methods for the month mapping (e.g., using a map or dictionary instead of the string index method in the Python example). The overall time and space complexity remain the same.