Maximize the missing values in a given time in HH:MM format


A string will be given to us of length five which represents the time in the HH:MM format. There may be some ‘?’ present in the string and we have to replace them with any number such that the result is the valid time which could be the maximum possible. Also, the given string numbers will be valid, and ‘:’ will be present at the exact position of the string. We will use the two approaches first the brute force, and another the efficient approach.

Sample Examples

Input 1

Given string: 12:5?
Output: 12:59

Explanation

We have only one place to fill and the maximum time we can get is 12:59.

Input 2

Given string: ?0:?9
Output: 20:59

Explanation

We have two empty slots here first, we will focus on the hour part, we have three choices 0,1, and 2 to fill. For the minutes part, we have choices from 0 to 5 and to maximize we can fill 5.

Approach

We have seen the examples, and now let’s see the different types of cases we can face −

  • We have two parts of the string hour part and the minutes, part.

  • Hour part is in the range of 0 to 23 and hour part is in the range of 0 to 59.

  • The hour part has further cases −

    • ‘x?’ where x can be 0,1, and 2. For 0 we can use 0 as the best, for 1 we can choose 9 as the best, and for 2 we can use 3 as the best.

    • ‘?x’ where x can be in the range of 0 to 9. If the x is in the range of 0 to 3 then we can replace it with 2 otherwise 1 will be the best.

    • ‘??’ as we have to maximize then we will replace it with the 23.

  • The minutes, part has further cases −

    • ‘x?’ where x can be in the range 0 to 5. 9 will be our best hit to replace ‘?’.

    • ‘?x’ where x can be in the range of 0 to 9. 5 will be our best hit to replace ‘?’.

    • ‘??’ as we have to maximize then we will replace it with the 59.

Let us see the code to implement the above steps −

Example

#include <iostream>
using namespace std;
// function to replace hours
string replaceHours(string s){
   if(s[0] == '?' && s[1] == '?'){
      //Both hour characters are '?'
      // replace with the maximum hour we can achieve 
      s[0] = '2';
      s[1] = '3';
   }
   else if(s[0] == '?'){
      // if the second number of hours is in the range 0 to 3
      // replace by 2
      if(s[1] < 4){
         s[0] = '2';
      }
      else{
         s[0] = '1'; // otherwise replace by one 
      }
   }
   else if(s[1] == '?'){
      // if the first character is '2' we can go only upto 3
      if(s[0] == '2'){
         s[1] = '3';
      }
      else{
         s[1] = '9'; // else we can go for 9 
      }
   }
   return s;
}
// function to replace minutes
string replaceMinutes(string s){
   if(s[3] == '?' && s[4] == '?'){
      // both minutes characters are '?'
      // replace with maximum minutes we can acheive 
      s[3] = '5';
      s[4] = '9';
   }
   else if(s[3] == '?'){
      // we can maximum get 5 here 
      s[3] = '5';
   }
   else if(s[4] == '?'){
      // we can get maximum 9 here
      s[4] = '9';
   }
   return s;
}
int main(){
   string str = "2?:3?"; // given string 
   // calling the function for updation of the minutes 
   str = replaceMinutes(str);
   // calling to the function for updation of the hours
   str = replaceHours(str);
   // printing the final answer
   cout<<"The maximum time we can get by replacing ? is: "<< str<<endl;
   return 0;
}

Output

The maximum time we can get by replacing ? is: 23:39

Time and Space Complexity

The time complexity of the above code is O(1) or constant, as we are not using any loop or the recursive calls, just checking the if-else condition

The space complexity of the above code is O(1), as we are not using any extra space. Also, for the function we are passing the string but its size is always constant 5.

Note: To make the code more good-looking or readable, switch cases can be used, they will not impact the time, or space complexity and will make it more efficient to read.

Also, back tracking and re-checking is one solution but that will check every case and is not that much efficient to implement here.

Conclusion

In this tutorial, we were given a string that represents the time in the 24-hours format. There are some ‘?’ present in the string which we have to replace to get the valid maximum time and it is guaranteed the given character in the string always leads to the valid time. We have used the if-else conditions and two functions to replace the ‘?’ with the appropriate cases. The time and space complexity of the above code is constant as we are not using any loops or recursion functions.

Updated on: 16-May-2023

71 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements