[LeetCode] Number of Elapsed Seconds Between Two Times

3986. Number of Elapsed Seconds Between Two Times

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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
class Solution {
array<int,3> parse(string& s) {
auto digitOf = [&](char ch) {
return ch - '0';
};
return {digitOf(s[0]) * 10 + digitOf(s[1]), digitOf(s[3]) * 10 + digitOf(s[4]), digitOf(s[6]) * 10 + digitOf(s[7])};
}
long long time(string& str) {
auto [h,m,s] = parse(str);
return h * 3600 + m * 60 + s;
}
public:
int secondsBetweenTimes(string startTime, string endTime) {
return time(endTime) - time(startTime);
}
};
Author: Song Hayoung
Link: https://songhayoung.github.io/2026/09/04/PS/LeetCode/number-of-elapsed-seconds-between-two-times/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.