Problem Description
Write a program to count the number of days between two valid dates provided in the format YYYY-MM-DD.
Key Insights
- Convert date strings into year, month, and day components.
- Calculate the total number of days since a fixed reference point (e.g., January 1, 1971) for each date.
- Use the helper function to account for leap years and the varying number of days in each month.
- Return the absolute difference between the total days of the two dates.
Space and Time Complexity
Time Complexity: O(1) - The calculation involves a fixed number of operations regardless of the input. Space Complexity: O(1) - Only a constant amount of extra space is utilized.
Solution
The key idea of the solution is to convert each date into a count of days from a fixed reference date (January 1, 1971). This is achieved by summing:
- The number of days for each full year since the reference year.
- The number of days for the complete months of the current year (taking leap years into account).
- The days in the current month. Once both dates are converted into total day counts, the absolute difference between these counts gives the number of days between the dates.
The approach efficiently handles the varying lengths of months and leap years within the given constraints.