1507. Reformat Date LeetCode Solution
In this guide, you will get 1507. Reformat Date LeetCode Solution with the best time and space complexity. The solution to Reformat Date problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.
Table of Contents
- Problem Statement
- Complexity Analysis
- Reformat Date solution in C++
- Reformat Date solution in Java
- Reformat Date solution in Python
- Additional Resources
Problem Statement of Reformat Date
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.
Complexity Analysis
- Time Complexity: O(1)
- Space Complexity: O(1)
1507. Reformat Date LeetCode Solution in C++
class Solution {
public:
string reformatDate(string date) {
const unordered_map<string, string> monthToNumString{
{"Jan", "01"}, {"Feb", "02"}, {"Mar", "03"}, {"Apr", "04"},
{"May", "05"}, {"Jun", "06"}, {"Jul", "07"}, {"Aug", "08"},
{"Sep", "09"}, {"Oct", "10"}, {"Nov", "11"}, {"Dec", "12"},
};
const int index1 = date.find_first_of(' ');
const int index2 = date.find_first_of(' ', index1 + 1);
const string day = index1 == 4 ? date.substr(0, 2) : string("0") + date[0];
const string month =
monthToNumString.at(date.substr(index1 + 1, index2 - (index1 + 1)));
const string year = date.substr(index2 + 1);
return year + "-" + month + "-" + day;
}
};
/* code provided by PROGIEZ */
1507. Reformat Date LeetCode Solution in Java
class Solution {
public String reformatDate(String date) {
Map<String, String> months = getMonths();
String[] words = date.split("\\s+");
final String day =
(words[0].length() == 4) ? words[0].substring(0, 2) : "0" + words[0].substring(0, 1);
final String month = months.get(words[1]);
final String year = words[2];
return year + "-" + month + "-" + day;
}
private 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;
}
}
// code provided by PROGIEZ
1507. Reformat Date LeetCode Solution in Python
class Solution:
def reformatDate(self, date: str) -> str:
monthToNumString = {
'Jan': '01', 'Feb': '02', 'Mar': '03', 'Apr': '04',
'May': '05', 'Jun': '06', 'Jul': '07', 'Aug': '08',
'Sep': '09', 'Oct': '10', 'Nov': '11', 'Dec': '12',
}
day, month, year = date.split()
day = day[:-2] if len(day) == 4 else '0' + day[:-2]
return f'{year}-{monthToNumString[month]}-{day}'
# code by PROGIEZ
Additional Resources
- Explore all LeetCode problem solutions at Progiez here
- Explore all problems on LeetCode website here
Happy Coding! Keep following PROGIEZ for more updates and solutions.