NINJA’S WATCH

Moderate
0/80
Average time to solve is 15m
profile
Contributed by
0 upvote
Asked in companies
IBMUber

Problem statement

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.

Detailed explanation ( Input/output format, Notes, Images )

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.
Note:
You are not required to print anything explicitly. It has already been taken care of. Just implement the given function.

Constraints:

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

Time Limit: 1 sec

Sample Input 1:

2
19:34
23:59

Sample Output 1:

19:39
22:22

Explanation of Sample Input 1:

Test Case 1:
According to this test case string given is ‘19:34’ so the possible time combination is:
For the same day, the next closet time is ‘19:39’, ‘19:41’, 19:43’
For the next day, the next closet time is ‘11:11’, ‘11:13’, ‘11:14’, ‘11:19’, ‘11:31’, ‘11:34’, ‘11:41’, ‘11:43’and so on.
So next time which is near to 19:34 is 19:39 so we return this as our answer.

Test Case 2:
According to this test case string given is ‘23:59’ so possible time combination is only possible for the next day i.e ‘22:22’, ‘22:23’, ‘22:25’, ‘22:29’, ‘22:32’and so on.
 So next time which is near 23:59 is 22:22 so we return this as our answer.

Sample Input 2:

2
11:34
07:24

Sample Output 2:

11:41
07:27
Hint

Can you find all possible combinations of time by using these given 4 digits?

Approaches (2)
Brute Approach
  • 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.
Time Complexity

O(1), constant time complexity.

 

As maximum, our loop will run maximum for 24*60=1440 making complexity as O(1440).

Space Complexity

O(1), constant space complexity.

 

As we are using constant space.

Code Solution
(100% EXP penalty)
NINJA’S WATCH
Full screen
Console