Suppose we have a string s. Here s is representing a 12-hour clock time with suffixes am or pm, we have to find its 24-hour equivalent.
So, if the input is like "08:40pm", then the output will be "20:40"
To solve this, we will follow these steps −
hour := (convert the substring of s [from index 0 to 2] as integer) mod 12
minutes := convert the substring of s [from index 3 to 5] as integer
if s[5] is same as 'p', then
hour := hour + 12
return the result as hour:minutes
Let us see the following implementation to get better understanding −
Example
class Solution:
def solve(self, s):
hour = int(s[:2]) % 12
minutes = int(s[3:5])
if s[5] == 'p':
hour += 12
return "{:02}:{:02}".format(hour, minutes)
ob = Solution()
print(ob.solve("08:40pm"))Input
"08:40pm"
Output
20:40