Blogs Hub

Reformat Date - String - Easy - LeetCode - ShotCut

Reformat Date - String - Easy - LeetCode - शॉटकट

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.

public class Solution {
    public string ReformatDate(string date) {
        
        var arr = date.Split(' ');
        var sb = new StringBuilder();
        
        sb.Append(arr[2]);
        sb.Append("-");
        sb.Append(GetMonth(arr[1]));
        sb.Append("-");
        
        if(Char.IsDigit(arr[0][1])){
            sb.Append(arr[0].Substring(0,2));
        } 
        else{
            sb.Append("0"+arr[0][0]);
        }
        
        return sb.ToString();
    }
    
    private string GetMonth(string month){        
        switch(month){
            case "Jan":
                return "01";
            case "Feb":
                return "02";
            case "Mar":
                return "03";
            case "Apr":
                return "04";
            case "May":
                return "05";
            case "Jun":
                return "06";
            case "Jul":
                return "07";
            case "Aug":
                return "08";
            case "Sep":
                return "09";
            case "Oct":
                return "10";
            case "Nov":
                return "11";
            case "Dec":
                return "12";
        }
        return "";
    }
}

Time Complexity: O(1)

Space Complexity: O(n)