Domain 1EasyCORE

Reformat Date

The problem requires converting a date string from the format `Day Month Year` to `YYYY-MM-DD`. The inputs are: - `Day`: A string representing the day with a suffix like "st", "nd", "rd", or "th". - `Month`: A three-letter abbreviation of the month. - `Year`: A four-digit number.

Domain: Easy•Updated: 2026-08-31
#Algorithms#LeetCode#qwen2.5-coder:14b#String

1507. Reformat Date

Model: qwen2.5-coder:14b
Topics: String
FAANG asked: 0
Acceptance rate: 60.1%


Problem Breakdown

The problem requires converting a date string from the format Day Month Year to YYYY-MM-DD. The inputs are:

  • Day: A string representing the day with a suffix like “st”, “nd”, “rd”, or “th”.
  • Month: A three-letter abbreviation of the month.
  • Year: A four-digit number.

The output should be a date string in the format YYYY-MM-DD. The constraints ensure that all inputs are valid, so no error handling is necessary. Edge cases include:

  • Single-digit days (e.g., “1st” instead of “01”).
  • Months with three-letter abbreviations.
  • Years within a specific range.

Intuition

A brute-force approach would involve manually parsing the string and converting each part to the desired format, which could be error-prone. A better approach is to split the input string into its components (day, month, year), normalize them, and then reformat them. The key insight is recognizing that the day component needs special handling due to its suffix.

Approach

  1. Split the Input: Use the split() method to break the input string into three parts: day, month, and year.
  2. Normalize the Day: Remove the suffix from the day string and ensure it is two digits by padding with a leading zero if necessary.
  3. Map Month Abbreviations: Create a dictionary to map month abbreviations to their corresponding two-digit numbers.
  4. Reformat the Date: Combine the year, month, and normalized day into the YYYY-MM-DD format.

Complexity Analysis

  • Time Complexity: O(1) since the operations (splitting, string manipulation, and dictionary lookup) are constant time.
  • Space Complexity: O(1) as we only use a fixed amount of extra space for variables and the month mapping.

Solution

def reformatDate(date: str) -> str:
    # Split the input date into day, month, and year
    day_str, month_str, year_str = date.split()
    
    # Normalize the day by removing the suffix and padding with a leading zero if necessary
    day_num = int(day_str[:-2])
    day = f"{day_num:02d}"
    
    # Map month abbreviations to their corresponding two-digit numbers
    month_map = {
        "Jan": "01", "Feb": "02", "Mar": "03", "Apr": "04",
        "May": "05", "Jun": "06", "Jul": "07", "Aug": "08",
        "Sep": "09", "Oct": "10", "Nov": "11", "Dec": "12"
    }
    month = month_map[month_str]
    
    # Reformat the date into YYYY-MM-DD
    return f"{year_str}-{month}-{day}"

Edge Cases Handled

  • Single-digit Days: The code uses f"{day_num:02d}" to ensure days are always two digits.
  • Month Abbreviations: A dictionary maps month abbreviations to their numeric representations, handling all valid cases.
  • Year Range: The year is directly used without modification, as it is already in the correct format.