Posted: 19 Mar, 2021
Difficulty: Moderate


Try Problem

Ninja has his own philosophy. He thinks that if his watch started showing next closet time he will be able to do all his work on time. As if his watch shows next closet time, then he would think that the deadline is near and he will be able to do his work before deadline.

So your task is to write a code that can return the next closet time you are given a time represented in the format “HH: MM” by using the current digits. There is no limit on how many times a digit can be used.

Input Format:

The first line of input contains a ‘T’ number of test cases.

The first line of input of each test case contains a string 'TIME', which represents the time in “HH: MM” format.

Output Format:

For each test case, return the string which is the next closest time to the given string.
You are not required to print anything explicitly. It has already been taken care of. Just implement the given function.


1 <= T <= 100 
| TIME | = 5

Time Limit: 1 sec
Approach 1
  • In this approach, we increase our time minute by minute like we first split our hour and minute in a different variable, and in each loop, we increase our minute by ‘1’ and if minute becomes greater than ‘60’ we increase hour by ‘1’.
  •  Now we check if such time uses the same set of digits we return that time.
  • We first store our time string in set ‘S’.
  • Now by using the function substring we store our hour in the form of integer in the variable ‘HOUR’ and minute in the ‘MIN’ variable.
  • Now we run a loop that continuously increases the ‘MIN’ variable by one.
  • Now we again convert our variable hour and min in the form of a string and stored them in set ‘S1’.
  • Now we check both sets if all the characters match we return our string else we continue our loop.
Try Problem