Given the start time st and end time et of a race in the format hh:mm:ss. You task is to print the time taken to complete the race.
Example 1:
Input: st = 13:50:45
et = 14:55:50
Output: 01:05:05
Explaination: The time gap is 1 hour 5 
minutes and 5 seconds.
Example 2:
Input: st = 12:00:00
et = 24:00:00
Output: 12:00:00
Explaination: The time gap is of 12 hours.
Your Task:
You do not need to read input or print anything. Your task is to complete the function timeGap() which takes st and et as input parameters and returns the time difference between them.
Expected Time Complexity: O(1)
Expected Auxiliary Space: O(1)
Constraints:
0 ≤ hh ≤ 24
0 ≤ mm ≤ 59
0 ≤ ss ≤ 59

Source: geeksforgeeks | Difficulty: EASY
Tags: String algorithms

Data structures

Mathematics

Starter code: #User function Template for python3

class Solution:
    def timeGap(self, st, et):
        # code here

Solutions:
import time

class Solution:

	def timeGap(self, st, et):
		l1 = st.split(':')
		l2 = et.split(':')
		l1[0] = str(int(l1[0]) * 3600)
		l1[1] = str(int(l1[1]) * 60)
		l2[0] = str(int(l2[0]) * 3600)
		l2[1] = str(int(l2[1]) * 60)
		total_seconds1 = int(l1[0]) + int(l1[1]) + int(l1[2])
		total_seconds2 = int(l2[0]) + int(l2[1]) + int(l2[2])
		total = total_seconds2 - total_seconds1
		res = time.strftime('%H:%M:%S', time.gmtime(total))
		return res


class Solution:

	def timeGap(self, st, et):
		arr = list(map(int, st.split(':')))
		start = 60 * 60 * arr[0] + 60 * arr[1] + arr[2]
		brr = list(map(int, et.split(':')))
		end = 60 * 60 * brr[0] + 60 * brr[1] + brr[2]
		time = end - start
		output = []
		output.append(str(time % 60))
		time //= 60
		output.append(str(time % 60))
		time //= 60
		output.append(str(time))
		output.reverse()
		for (i, item) in enumerate(output):
			if len(item) == 1:
				output[i] = '0' + item
		return ':'.join(output)


class Solution:

	def getTimeInSecond(stri):
		curtime = stri.split(':')
		t = int(curtime[0]) * 60 * 60 + int(curtime[1]) * 60 + int(curtime[2])
		return t

	def convertSecToTime(t):
		hours = t // 3600
		if hours < 10:
			hh = '0' + str(hours)
		else:
			hh = str(hours)
		mint = t % 3600 // 60
		if mint < 10:
			mm = '0' + str(mint)
		else:
			mm = str(mint)
		sec = t % 3600 % 60
		if sec < 10:
			ss = '0' + str(sec)
		else:
			ss = str(sec)
		ans = hh + ':' + mm + ':' + ss
		return ans

	def timeGap(self, st, et):
		t1 = Solution.getTimeInSecond(st)
		t2 = Solution.getTimeInSecond(et)
		if t1 - t2 < 0:
			timediff = t2 - t1
		else:
			timediff = t1 - t2
		return Solution.convertSecToTime(timediff)


class Solution:

	def timeGap(self, st, et):
		st = st.split(':')
		et = et.split(':')
		st_time = 3600 * int(st[0]) + 60 * int(st[1]) + int(st[2])
		et_time = 3600 * int(et[0]) + 60 * int(et[1]) + int(et[2])
		tt = (et_time - st_time) // 3600
		ans = ''
		if tt > 9:
			ans += str(tt)
		elif tt > 0:
			ans += '0' + str(tt)
		else:
			ans += '00'
		ans += ':'
		tt = (et_time - st_time) % 3600 // 60
		if tt > 9:
			ans += str(tt)
		elif tt > 0:
			ans += '0' + str(tt)
		else:
			ans += '00'
		ans += ':'
		tt = (et_time - st_time) % 3600 % 60
		if tt > 9:
			ans += str(tt)
		elif tt > 0:
			ans += '0' + str(tt)
		else:
			ans += '00'
		return ans


class Solution:

	def timeGap(self, st, et):
		st = st.split(':')
		et = et.split(':')
		st_time = 3600 * int(st[0]) + 60 * int(st[1]) + int(st[2])
		et_time = 3600 * int(et[0]) + 60 * int(et[1]) + int(et[2])
		tt = (et_time - st_time) // 3600
		ans = ''
		if tt > 9:
			ans += str(tt)
		elif tt > 0:
			ans += '0' + str(tt)
		else:
			ans += '00'
		ans += ':'
		tt = (et_time - st_time) % 3600 // 60
		if tt > 9:
			ans += str(tt)
		elif tt > 0:
			ans += '0' + str(tt)
		else:
			ans += '00'
		ans += ':'
		tt = (et_time - st_time) % 3600 % 60
		if tt > 9:
			ans += str(tt)
		elif tt > 0:
			ans += '0' + str(tt)
		else:
			ans += '00'
		return ans
if __name__ == '__main__':
	t = int(input())
	for _ in range(t):
		st = input()
		et = input()
		ob = Solution()
		print(ob.timeGap(st, et))


class Solution:

	def timeGap(self, st, et):
		s = ''
		start = list(map(int, st.split(':')))
		end = list(map(int, et.split(':')))
		ans = []
		for i in range(len(start) - 1, -1, -1):
			if start[i] <= end[i]:
				ans.append(end[i] - start[i])
			else:
				end[i - 1] -= 1
				end[i] += 60
				ans.append(end[i] - start[i])
		s = ''
		for i in range(len(ans) - 1, -1, -1):
			if ans[i] <= 9:
				s += '0' + str(ans[i])
			else:
				s += str(ans[i])
			if i != 0:
				s += ':'
		return s