1360. Number of Days Between Two Dates LeetCode Solution
In this guide, you will get 1360. Number of Days Between Two Dates LeetCode Solution with the best time and space complexity. The solution to Number of Days Between Two Dates 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
- Number of Days Between Two Dates solution in C++
- Number of Days Between Two Dates solution in Java
- Number of Days Between Two Dates solution in Python
- Additional Resources
Problem Statement of Number of Days Between Two Dates
Write a program to count the number of days between two dates.
The two dates are given as strings, their format is YYYY-MM-DD as shown in the examples.
Example 1:
Input: date1 = “2019-06-29”, date2 = “2019-06-30”
Output: 1
Example 2:
Input: date1 = “2020-01-15”, date2 = “2019-12-31”
Output: 15
Constraints:
The given dates are valid dates between the years 1971 and 2100.
Complexity Analysis
- Time Complexity: O(1)
- Space Complexity: O(1)
1360. Number of Days Between Two Dates LeetCode Solution in C++
class Solution {
public:
int daysBetweenDates(string date1, string date2) {
return abs(daysFrom1971(date1) - daysFrom1971(date2));
}
private:
const vector<int> days{0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
int daysFrom1971(const string& date) {
const int year = stoi(date1.substr(0, 4));
const int month = stoi(date1.substr(5, 2));
const int day = stoi(date1.substr(8, 2));
int res = 0;
for (int i = 1971; i < year; ++i)
res += isLeapYear(i) ? 366 : 365;
for (int i = 0; i < month; ++i)
res += days[i];
if (month > 2 && isLeapYear(year))
++res;
res += day;
return res;
}
bool isLeapYear(int year) {
return year % 4 == 0 && year % 100 != 0 || year % 400 == 0;
};
};
/* code provided by PROGIEZ */
1360. Number of Days Between Two Dates LeetCode Solution in Java
class Solution {
public int daysBetweenDates(String date1, String date2) {
return Math.abs(daysFrom1971(date1) - daysFrom1971(date2));
}
private static final int[] days = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
private int daysFrom1971(final String date) {
final int year = Integer.valueOf(date.substring(0, 4));
final int month = Integer.valueOf(date.substring(5, 7));
final int day = Integer.valueOf(date.substring(8));
int res = 0;
for (int i = 1971; i < year; ++i)
res += isLeapYear(i) ? 366 : 365;
for (int i = 0; i < month; ++i)
res += days[i];
if (month > 2 && isLeapYear(year))
++res;
return res + day;
}
private boolean isLeapYear(int year) {
return year % 4 == 0 && year % 100 != 0 || year % 400 == 0;
};
}
// code provided by PROGIEZ
1360. Number of Days Between Two Dates LeetCode Solution in Python
class Solution:
def daysBetweenDates(self, date1: str, date2: str) -> int:
days = [0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
def isLeapYear(year: int) -> bool:
return year % 4 == 0 and year % 100 != 0 or year % 400 == 0
def daysFrom1971(date: str) -> int:
year, month, day = map(int, date.split('-'))
return (365 * (year - 1971) + sum(map(isLeapYear, range(1971, year))) +
sum(days[:month]) + day + (month > 2 and isLeapYear(year)))
return abs(daysFrom1971(date1) - daysFrom1971(date2))
# 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.