Number of Elapsed Seconds Between Two Times - Solution & Explanation
Problem Statement
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
Approach Overview
Problem Overview: You are given two times and need to compute how many seconds elapsed between them. The cleanest solution converts each timestamp into total seconds, then subtracts the values.
Approach 1: Increment Second-by-Second Simulation (O(diff) time, O(1) space)
You can simulate time progression from the start timestamp until it matches the end timestamp, incrementing a counter every second. This approach mirrors how clocks work internally and helps verify edge cases such as minute and hour rollover. The downside is scalability because the runtime depends directly on the number of elapsed seconds. It is mainly useful as a conceptual baseline or for validating small inputs.
Approach 2: Convert Time to Total Seconds (O(1) time, O(1) space)
Convert each time into the number of seconds since 00:00:00 using the formula hours * 3600 + minutes * 60 + seconds. Once both values are computed, subtract the earlier timestamp from the later one. This avoids loops entirely and reduces the problem to simple arithmetic operations. This is the expected optimal solution because the number of operations stays constant regardless of the input values.
The core idea relies on direct arithmetic instead of iterative traversal. Problems like this often appear under math and simulation categories because you model time mathematically rather than storing intermediate states. You only need primitive integer variables, so memory usage remains constant.
Approach 3: Parse and Normalize Time Components (O(1) time, O(1) space)
If the input arrives as formatted strings such as HH:MM:SS, first split the string into components and parse integers. After normalization, apply the same total-seconds conversion. This version is common in production systems where timestamps are serialized text instead of structured arrays or integers. The arithmetic remains identical after parsing.
Recommended for interviews: Interviewers expect the total-seconds conversion approach because it demonstrates that you can simplify a real-world process into constant-time arithmetic. Mentioning the simulation approach first shows you understand the brute-force interpretation, but deriving the direct conversion formula shows stronger problem-solving ability. Similar optimizations appear frequently in implementation problems.
Solution
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).
Code
Python
Java
C++
Go
TypeScript
Detailed Complexity Analysis
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Second-by-Second Simulation | O(diff) | O(1) | Useful for understanding clock transitions and validating logic |
| Convert to Total Seconds | O(1) | O(1) | Best general solution with minimal operations |
| Parse and Normalize Strings | O(1) | O(1) | When timestamps are provided in HH:MM:SS format |
Video Solution
3986. Number of Elapsed Seconds Between Two Times (Leetcode Easy) • Programming Live with Larry • 175 views views
Watch 4 more video solutions →Frequently Asked Questions
Is Number of Elapsed Seconds Between Two Times easy or hard?
Number of Elapsed Seconds Between Two Times Python/Java solution
How to solve Number of Elapsed Seconds Between Two Times in O(1)?
What is the best approach for Number of Elapsed Seconds Between Two Times?
Is Number of Elapsed Seconds Between Two Times asked at Google/Amazon/Meta?
What data structure is used in Number of Elapsed Seconds Between Two Times?
What is the time complexity of Number of Elapsed Seconds Between Two Times?
Ready to solve this problem?
Practice Number of Elapsed Seconds Between Two Times with our built-in code editor and test cases.
Practice on FleetCodeProblem Info
Table of Contents
Practice this problem
Open in Editor