Reformat Date LeetCode Solution

Difficulty Level Easy
Frequently asked in Apple Expedia Twilio
tiktok Walmart Global techViews 3464

Problem Statement

Reformat Date LeetCode Solution – 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.

Reformat Date LeetCode Solution

Example

Test Case 1:

Input:

date = “20th Oct 2052”

Output:

“2052-10-20”

Test Case 2:

Input:

date = “26th May 1960”

Output:

“1960-05-26”

Approach:

There are 3 parts that we can extract easily by splitting by white space:

  • Year: already in the format that we want.
  • Month: needs translation from string to numeric representation. We can use a Map for that.
  • Day: it’s mixed with some suffixes (e.g. stnd). We can just remove those using parseInt.

This is the end result.

Ah, we also need to add zero padding. So if we have a day like 1, we want it to be 01.

Below are the steps:

  1. From the first two letters take only numbers, if it is one letter then append 0. At max O(2) run time
  2. O(1) to get the month number.
  3. All last 4 digits of the year.
  4. Add all those in order and return.

Code for Reformat Date

C++ Program

class Solution {
public:
    string reformatDate(string date) {
        string res;
        if(date.length()==13)
            res=date.substr(9,4)+'-'+ret(date.substr(5,3))+'-'+date.substr(0,2);
        else
            res=date.substr(8,4)+'-'+ret(date.substr(4,3))+'-'+'0'+date.substr(0,1);
        return res;
    }
    string ret(string s){
        if(s=="Jan")    return "01";
        else if(s=="Feb")   return "02";
        else if(s=="Mar")   return "03";
        else if(s=="Apr")   return "04";
        else if(s=="May")   return "05";
        else if(s=="Jun")   return "06";
        else if(s=="Jul")   return "07";
        else if(s=="Aug")   return "08";
        else if(s=="Sep")   return "09";
        else if(s=="Oct")   return "10";
        else if(s=="Nov")   return "11";
        else        return "12";
    }
};

Java Program

class Solution {
    private static final Map<String, String> months = getMonths();
    public String reformatDate(String date) {
        String[] ss = date.split("\\s+");
        StringBuilder sb = new StringBuilder();
        sb.append(ss[2]).append("-");
        sb.append(months.get(ss[1])).append("-");
        sb.append(ss[0].length() == 3 ? ("0" + ss[0].substring(0, 1)) : ss[0].substring(0, 2));
        return sb.toString();
    }
    
    private static Map<String, String> getMonths(){
        Map<String, String> months = new HashMap<>();
        months.put("Jan", "01");
        months.put("Feb", "02");
        months.put("Mar", "03");
        months.put("Apr", "04");
        months.put("May", "05");
        months.put("Jun", "06");
        months.put("Jul", "07");
        months.put("Aug", "08");
        months.put("Sep", "09");
        months.put("Oct", "10");
        months.put("Nov", "11");
        months.put("Dec", "12");
        return months;
    }
}

Complexity Analysis for Reformat Date LeetCode Solution

Time Complexity: O(1), as we are using constant time.

Space Complexity: O(1), as we are using constant space.

Translate »