Code360 powered by Coding Ninjas X Code360 powered by Coding Ninjas X
Last Updated: 29 Jul, 2021

Maximum Time

Asked in company

Problem statement

You are given a string that represents time in the format hh:mm. Some of the digits are blank (represented by ‘?’). Fill in ‘?’ such that the time represented by this string is the maximum possible. Maximum time: 23:59, minimum time: 00:00. You can assume that the input string is always valid.

For Example :
If the given input string is 1?:?8.
We can replace the first ‘?’ with 9 and second with 5 to get the maximum time of 19:58.
Input Format :
 The first line contains a single integer ‘T’ denoting the number of test cases Then each test case follows.
 The first line of each test case contains a string in the format of ‘hh:mm’.
Output Format :
 For each test case print a string denoting the maximum possible time after replacing the ‘?’ marks with numbers..

Output for each test case will be printed in a separate line.
Note :

You are not required to print anything; it has already been taken care of. Just implement the function.

Constraints :
1 <= T <= 1000     
|S| = 5

Time limit: 1 sec


01 Approach

Let’s try to build the string character by character from left to right.

  • If the 0th character is ‘?’
    • If the next character can be less than 3 or ‘?’ we can place a ‘2’ here.
    • Else we are bound to place a ‘1’.
  • If the first character is ‘?’,
    • If the 0th character is less than ‘2’ we can place the character ‘9’.
    • Else we place the character ‘3’.
  • If the third character is ‘?’,
    • We place a ‘5’ here.
  • If the last character is ‘?’,
    • We place a ‘9’ here.