Similar Problems

Similar Problems not available

Reformat Date - Leetcode Solution

LeetCode:  Reformat Date Leetcode Solution

Difficulty: Easy

Topics: string  

Problem Statement:

Given a date string in the format 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 a 4-digit number. 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.

Solution:

The solution to this problem can be approached in a few simple steps:

  1. Split the date string into Day, Month, and Year using string methods.

  2. Convert the Month to its equivalent 2-digit number.

  3. Convert the Day to its equivalent 2-digit number.

  4. Concatenate the Year, Month, and Day with hyphens to form the final date string.

Now let's write the code for each of these steps.

Step 1: Split the date string

We can split the date string using the split() method in python. The split() method returns a list of strings that are separated by a separator. In this case, the separator is a space.

split the date string

date_str = "20th Oct 2052" date_list = date_str.split()

date_list = ["20th", "Oct", "2052"]

day = date_list[0] month = date_list[1] year = date_list[2]

Step 2: Convert the Month to its equivalent 2-digit number

We can create a dictionary that maps each month to its equivalent 2-digit number. Then we can use this dictionary to convert the month string to its equivalent 2-digit number.

create a dictionary that maps month to its equivalent number

month_dict = { "Jan": "01", "Feb": "02", "Mar": "03", "Apr": "04", "May": "05", "Jun": "06", "Jul": "07", "Aug": "08", "Sep": "09", "Oct": "10", "Nov": "11", "Dec": "12" }

convert month to its equivalent 2-digit number

month = month_dict[month]

Step 3: Convert the Day to its equivalent 2-digit number

We can use string methods to check if the day string ends with "st", "nd", "rd", or "th". Then we can use slicing to remove these suffixes and get the numeric value of the day. We can also add a leading zero if the day is a single digit.

check if the day string ends with "st", "nd", "rd", or "th"

if day.endswith("st") or day.endswith("nd") or day.endswith("rd") or day.endswith("th"): # remove the suffix from the day string day = day[:-2]

add leading zero if the day is a single digit

if len(day) == 1: day = "0" + day

Step 4: Concatenate the Year, Month, and Day with hyphens

We can concatenate the Year, Month, and Day with hyphens to form the final date string.

concatenate Year, Month and Day with hyphens

date_str = f"{year}-{month}-{day}"

Final Code:

Putting all the above steps together, below is the final code to solve the problem:

class Solution: def reformatDate(self, date: str) -> str: # split the date string date_list = date.split()

    # get the day, month, and year
    day = date_list[0]
    month = date_list[1]
    year = date_list[2]

    # create a dictionary that maps month to its equivalent number
    month_dict = {
        "Jan": "01",
        "Feb": "02",
        "Mar": "03",
        "Apr": "04",
        "May": "05",
        "Jun": "06",
        "Jul": "07",
        "Aug": "08",
        "Sep": "09",
        "Oct": "10",
        "Nov": "11",
        "Dec": "12"
    }

    # convert month to its equivalent 2-digit number
    month = month_dict[month]

    # check if the day string ends with "st", "nd", "rd", or "th"
    if day.endswith("st") or day.endswith("nd") or day.endswith("rd") or day.endswith("th"):
        # remove the suffix from the day string
        day = day[:-2]

    # add leading zero if the day is a single digit
    if len(day) == 1:
        day = "0" + day

    # concatenate Year, Month and Day with hyphens
    date_str = f"{year}-{month}-{day}"

    return date_str

Time Complexity: O(1)

Space Complexity: O(1)

Reformat Date Solution Code

1