algoadvance

Leetcode 1507. Reformat Date

Problem Statement

Given a date string in a format of “Day Month Year”, where:

You are tasked with reformatting the date into the “YYYY-MM-DD” format.

Example

  1. Input: “20th Oct 2052”
    • Output: “2052-10-20”
  2. Input: “6th Jun 1933”
    • Output: “1933-06-06”
  3. Input: “26th May 1960”
    • Output: “1960-05-26”

Clarifying Questions

  1. Will the input always be in the correct “Day Month Year” format?
    • Assume yes.
  2. Are all month abbreviations three characters long?
    • Yes.
  3. Will the day always include a valid suffix (e.g., “1st”, “2nd”, “3rd”, “4th”)?
    • Yes.

Strategy

  1. Split the input date string into its constituent parts: day, month, and year.
  2. Extract the numerical part of the day by removing the suffix.
  3. Convert the three-letter month to its two-digit numerical representation.
  4. Format the date into “YYYY-MM-DD” format and return.

Code

#include <iostream>
#include <unordered_map>
#include <string>

using namespace std;

string reformatDate(string date) {
    unordered_map<string, string> monthMap = {
        {"Jan", "01"}, {"Feb", "02"}, {"Mar", "03"}, {"Apr", "04"},
        {"May", "05"}, {"Jun", "06"}, {"Jul", "07"}, {"Aug", "08"},
        {"Sep", "09"}, {"Oct", "10"}, {"Nov", "11"}, {"Dec", "12"}
    };

    string day, month, year;
    int i = 0;
    
    // Extract day part
    while (isdigit(date[i])) {
        day.push_back(date[i]);
        i++;
    }
    
    // Skip the suffix
    while (!isspace(date[i])) i++;
    i++;
    
    // Extract month part
    while (!isspace(date[i])) {
        month.push_back(date[i]);
        i++;
    }
    i++;
    
    // Extract year part
    while (i < date.size()) {
        year.push_back(date[i]);
        i++;
    }
    
    // Convert day to two digits
    if (day.size() == 1) day = "0" + day;
    
    // Get the month in two-digit format
    string monthNumerical = monthMap[month];
    
    return year + "-" + monthNumerical + "-" + day;
}

int main() {
    string date = "20th Oct 2052";
    cout << reformatDate(date) << endl;  // Output: "2052-10-20"

    return 0;
}

Time Complexity

The time complexity of this solution is O(1) because regardless of the length and content of the input (given the constraints), the operations involved (string parsing and mapping) are constant and do not depend on the size of the input.

Explanation

  1. Extracting Day, Month, and Year:
    • We iterate through the string to extract the numerical day until we encounter a non-digit.
    • Skip any characters until we find whitespace.
    • Extract the month until the next whitespace.
    • Extract the year until the end of the string.
  2. Reformatting Day and Month:
    • Convert day to a two-digit format by adding a leading zero if necessary.
    • Map the abbreviated month to its two-digit numerical counterpart using an unordered map.
  3. Output the reformatted date in “YYYY-MM-DD” format.

Cut your prep time in half and DOMINATE your interview with AlgoAdvance AI