You are preparing for google interviews. You came across a problem where you have a string ‘STR’, representing the time in ‘hh:mm’ (24 hr format). Some of the digits are blank, which is represented by ‘#’. Replace the ‘#’ such that the time represented by this string is the maximum possible.
Example :
Given ‘STR’ : #5:40
Maximum time possible can be : 15:40
The first line of input contains an integer ‘T’, the number of test cases.
The first line of each test case contains the string ‘STR’, representing the time.
Output Format :
For each test case, print a string representing the maximum possible time.
Output for each test case will be printed in a separate line.
Note :
You do not need to print anything, it has already been taken care of. Just implement the given function.
1 <= T <= 50
The string ‘STR’ is always valid.
Time Limit : 1 sec
2
#5:40
#4:#5
15:40
14:55
For first test case :
The maximum possible hour can be 15, and the maximum possible minutes can be 40.
For second test case :
The maximum possible hour can be 14, and the maximum possible minutes can be 55.
2
##:30
14:2#
23:30
14:29
Try finding all possible time.
The basic idea is to find all the possible times and return the maximum time out of those using recursion.
Here is the algorithm :
HELPER(STR, RES, IDX): (‘HELPER’ function)
isPossible(STR): (Function to check whether the time is possible or not).
findMax(STR, RES): (Function to find the maximum of ‘STR’ and ‘RES’).
O(1)
We assign digits to all the possible places of ‘STR’. The total possible number of strings that represents the possible time that can be made is 1050 i.e 3*5*7*10.
00:00, 00:01, 00:02...... 23:56, 23:57, 23:58, 23:59, 24:00.
O(480) ~ O(1)
But in code to make the implementation little easy we have taken maximum number that possible at last position.
There are 10 choices we can use for every 4 positions. So, the total number of combinations can be 10^4. For each possible combination, we check whether the time in the string is possible or not, which takes O(1) time, and we also find the maximum time which takes O(5) time. Therefore, the overall time complexity will be O((10^4)*5), i.e., O(1).
O(1)
We use string ‘RES’ to store the maximum possible time, whose length will be 5. The recursive stack can contain the size of the string ‘STR’, i.e., 5. Therefore, the overall space complexity will be O(1).