Number of Days Between Two Dates
Real-world version: Days Between Two Stamped SlipsThe statement here is our own: the same inputs, outputs and technique.
An archivist pulls two paper slips out of a box. Each slip carries the date it was stamped, and the archivist wants to know how far apart in whole days the two stampings were.
A stamp is written as YYYY-MM-DD: four digits for the year, a hyphen, two digits for the month, another hyphen, two digits for the day. 1985-04-07 is the seventh of April, 1985.
Given the two stamps slip_a and slip_b, return how many days one stamping was after the other. The slips come out of the box in no particular order, so slip_a may be the later of the two; the answer is never negative. Two slips stamped on the same day are zero days apart, and two slips stamped on consecutive days are one day apart.
Dates follow the Gregorian calendar. Months hold 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30 and 31 days in order, except that the second month holds 29 days in a leap year. A year is a leap year when it is divisible by 4, with one exception: a year divisible by 100 is a leap year only when it is also divisible by 400. So 1900 and 2100 are ordinary years while 1600, 2000 and 2400 are leap years.
Examples
Example 1
- Input
- slip_a = "2024-02-26", slip_b = "2024-03-05"
- Output
- 8
2024 is divisible by 4 and not by 100, so its second month runs to the 29th. Stepping from 26 February: 27, 28, 29 February, then 1, 2, 3, 4, 5 March, which is eight days.
Example 2
- Input
- slip_a = "2100-03-01", slip_b = "2100-01-01"
- Output
- 59
Here `slip_a` is the later stamp and the count is still reported unsigned. 2100 is divisible by 100 but not by 400, so its second month has 28 days: 31 days in the first month plus 28 in the second gets from 1 January to 1 March.
Example 3
- Input
- slip_a = "1998-11-30", slip_b = "1998-11-30"
- Output
- 0
Both slips carry the same stamp, so nothing separates them.
Constraints
- slip_a.length == 10
- slip_b.length == 10
- Each stamp is a valid Gregorian date written as YYYY-MM-DD.
- Both years lie between 1971 and 2100 inclusive.
The signature
The editor starts with this. It matches the parent problem's shape, so a solution that works here works on a standard judge unchanged.
def days_apart(slip_a: str, slip_b: str) -> int:public int daysApart(String slipA, String slipB)Checking your plan…
How this problem was checked
- Passed: Reference solution run against 15 test cases: 15 passed
- Passed: 3 worked examples confirmed by running the code
- Passed: 0 eight-word sequences shared with the parent problem's text
- Passed: Agrees with an independent brute-force solution on random inputs
- Passed: Test set shown to catch deliberately broken solutions
- Passed: Reviewed by a separate quality pass
- Passed: Function arity matches the parent problem's