LeetCode 1853: Convert Date Format Solution
Master LeetCode problem 1853 (Convert Date Format), a easy challenge, with our optimized solutions in Java, C++, and Python. Explore detailed explanations, test your code in our interactive editor, and prepare for coding interviews.
1853. Convert Date Format
Problem Explanation
Explanation:
To convert a date string from the format "YYYY-MM-DD" to "DD-MM-YYYY", we can split the original string by the delimiter "-", and then rearrange the parts accordingly.
Algorithm:
- Split the input date string by the delimiter "-" to extract the year, month, and day.
- Rearrange the parts to form the new date string in the required format "DD-MM-YYYY".
- Return the new date string.
Time Complexity:
The time complexity of this algorithm is O(1) because the input date string format is fixed.
Space Complexity:
The space complexity is also O(1) as we are not using any extra space that grows with the input size.
: :
Solution Code
class Solution {
public String reformatDate(String date) {
String[] parts = date.split("-");
return parts[2] + "-" + getMonth(parts[1]) + "-" + parts[0];
}
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";
default:
return "";
}
}
}Try It Yourself
Loading code editor...
Related LeetCode Problems
Frequently Asked Questions
How to solve LeetCode 1853 (Convert Date Format)?
This page provides optimized solutions for LeetCode problem 1853 (Convert Date Format) in Java, C++, and Python, along with a detailed explanation and an interactive code editor to test your code.
What is the time complexity of LeetCode 1853 (Convert Date Format)?
The time complexity for LeetCode 1853 (Convert Date Format) varies by solution. Check the detailed explanation section for specific complexities in Java, C++, and Python implementations.
Can I run code for LeetCode 1853 on DevExCode?
Yes, DevExCode provides an interactive code editor where you can write, test, and run your code for LeetCode 1853 in Java, C++, or Python.