3986. Number of Elapsed Seconds Between Two Times
Description
You are given two valid times startTime and endTime, each represented as a string in the format "HH:MM:SS".
Return the number of seconds that have elapsed from startTime to endTime.
Β
Example 1:
Input: startTime = "01:00:00", endTime = "01:00:25"
Output: 25
Explanation:
endTime is 25 seconds ahead of startTime.Example 2:
Input: startTime = "12:34:56", endTime = "13:00:00"
Output: 1504
Explanation:
endTime is 25 minutes and 4 seconds ahead of startTime, which equals 1504 seconds.
Β
Constraints:
startTime.length == 8endTime.length == 8startTimeandendTimeare valid times in the format"HH:MM:SS"00 <= HH <= 2300 <= MM <= 5900 <= SS <= 59endTimeis not earlier thanstartTime
Solutions
Solution 1: Simulation
Convert each time string into the number of seconds elapsed since \(00\):\(00\):\(00\), i.e. \(HH \times 3600 + MM \times 60 + SS\), then return the difference between the two values.
The time complexity is \(O(1)\), and the space complexity is \(O(1)\).
1 2 3 4 5 6 | |
1 2 3 4 5 6 7 8 9 10 | |
1 2 3 4 5 6 7 8 9 10 11 12 13 | |
1 2 3 4 5 6 7 8 9 10 | |
1 2 3 4 5 6 7 | |