Easy
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 == 8startTime and endTime are valid times in the format "HH:MM:SS"00 <= HH <= 2300 <= MM <= 5900 <= SS <= 59endTime is not earlier than startTimeimport kotlin.math.pow
class Solution {
fun secondsBetweenTimes(startTime: String, endTime: String): Int {
var startSec = 0
var endSec = 0
var sSec = 0
var eSec = 0
var colon = 2
for (i in 0..7) {
if (startTime[i] == ':') {
startSec = (startSec + sSec * 60.0.pow(colon.toDouble())).toInt()
endSec = (endSec + eSec * 60.0.pow(colon.toDouble())).toInt()
colon--
sSec = 0
eSec = 0
continue
}
sSec = sSec * 10 + (startTime[i].code - '0'.code)
eSec = eSec * 10 + (endTime[i].code - '0'.code)
}
startSec = (startSec + sSec * 60.0.pow(colon.toDouble())).toInt()
endSec = (endSec + eSec * 60.0.pow(colon.toDouble())).toInt()
return endSec - startSec
}
}