Time Conversion - HackerRank Problem Solving

Featured image

Time Conversion is a Hackerrank problem from Algorithms subdomain that requires the understanding of date and time manipulation. In this post, you will learn how to solve Hackerrank’s Time Conversion problem and its solution in Python and C++.

Problem Statement and Explanation

Convert the input 12-hour time format to 24-hour time format.

Input Format

Output Format

Staircase Solution in Python

Explanation of Solution in Python

timeConversion(), It takes a string representing a time in 12-hour format as input and returns a string representing the same time in 24-hour format.

The function works by first splitting the input string into a list of three strings, representing the hours, minutes, and seconds, respectively. It then checks the last two characters of the input string to see if they are “PM”. If they are, the function adds 12 to the hours value, unless the hours value is already 12, in which case it sets it to 0.

Next, the function joins the three strings back into a single string, using a colon as the separator. Finally, it returns the first two characters of the string, which is the time in 24-hour format.