Number of Days Between Two Dates LeetCode Solution

Difficulty Level Easy
Frequently asked in Amazon MicrosoftViews 3316

Problem Statement

The question Number of Days Between Two Dates LeetCode Solution asks us to calculate the exact number of days between 2 given dates including leap years. The dates are given as strings in the format YYYY-MM-DD. It is also given that the input dates are valid dates between the years 1971 and 2100.

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

Approach

This problem is a little tricky because we have to account for the leap years also.

Let’s recall the concept of Leap Year, a given year is a leap year if

  • it is divisible by 400 or
  • divisible by 4 and not divisible by 4

The idea to solve this problem is to count the number of days passed since 1971 and subtract the number of days present on both dates. Extract the year, month, and day out of the given string date using the inbuilt function. Loop over the years from 1971 to the present-1 year and check for leap year and add 365 or 366 accordingly.

For the present year, add days according to the months passed, keep an array to store the number of days present in the 12 months of a non-leap year.

We can use Java time API also for these calculations.

Code

C++ Code for Number of Days Between Two Dates

class Solution {
    int months[12]={31,28,31,30,31,30,31,31,30,31,30,31};
    
    bool isLeapYear(int year){
        return (year%400==0) || (year%100!=0 && year%4==0);
    }
    
    int calNumOfDays(string date){
        int year = stoi(date.substr(0,4));
        int month = stoi(date.substr(5,2));
        int days = stoi(date.substr(8,2));
        
        for(int yr=1971; yr<year; yr++){
            if(isLeapYear(yr)) days+=1;
            days+=365;
        }
        for(int mth=0; mth<month-1; mth++) {
            if(mth==1 && isLeapYear(year)) days+=1;
            days+=months[mth];
        }
        return days;
    }
public:
    int daysBetweenDates(string date1, string date2) {
        int numOfDaysinDate1 = calNumOfDays(date1);
        int numOfDaysinDate2 = calNumOfDays(date2);
        return abs(numOfDaysinDate1-numOfDaysinDate2);
    }
};

Java Code for Number of Days Between Two Dates

class Solution {
    int[] months = {31,28,31,30,31,30,31,31,30,31,30,31};
    
    private boolean isLeapYear(int year){
        return (year%400==0) || (year%100!=0 && year%4==0);
    }
    
    private int days(String d) {
        String[] ss = d.split("-");
        int year = Integer.valueOf(ss[0]);
        int month = Integer.valueOf(ss[1]);
        int days = Integer.valueOf(ss[2]);
        
        for(int yr=1971; yr<year; yr++){
            if(isLeapYear(yr)) days = days + 1;
            days = days + 365;
        }
        for(int mth=0; mth<month-1; mth++) {
            if(mth==1 && isLeapYear(year)) days = days + 1;
            days = days + months[mth];
        }
        return days;
    }
    
     public int daysBetweenDates(String date1, String date2) {
        int days1 = days(date1);
        int days2 = days(date2);
        return Math.abs(days1 - days2);
    }
}

Complexity Analysis for Number of Days Between Two Dates LeetCode Solution

Let n1 & n2 be the years in the date1 and date2, N=max(n1, n2)

  • Time Complexity: ~O(N-1971) = O(N)
  • Space Complexity: Since we defined month constant size O(1)

Reference: https://docs.oracle.com/javase/8/docs/api/java/time/package-summary.html

Translate »