Open In App

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

Last Updated : 23 Jul, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

Given a string S representing the time in the 24-hour format “HH:MM” such that some digits are represented by ‘?’, the task is to replace ‘?’ with any possible digits such that the resultant time is the maximum possible time. 

Examples:

Input: S = “?4:5?”
Output: 14:59
Explanation:
After replacing the first and the second ‘?’ with the digits 1 and 9, modifies the given time to “14:59”, which is maximum among all the possible time that can be made by replacing ‘?’.

Input: S = “0?:??”
Output: 09:59

Approach: The given problem can be solved by traversing the given string S and replace the ‘?’ in such a way that the substring before the character ‘:’ lies over the range [0, 23] and the substring after the ‘:’ must be at most 59 and print the maximum time obtained. Follow the below steps to solve the given problem:

  • If the value of the character S at the index 0 is ‘?’ and the character at the index 1 is ‘3’ or ‘?’, then update the value of S[0] as ‘2’. Otherwise, update the value of S[0] as ‘1’.
  • If the value of the character S at the index 1 is ‘?’ and the character at the index 0 is not ‘2’, then update the value of S[1] as ‘9’. Otherwise, update the value of S[1] as ‘3’.
  • If the value of the character S at the index 3 is ‘?’, then update the value of S[3] as ‘5’.
  • If the value of the character S at the index 4 is ‘?’, then update the value of S[4] as ‘9’.
  • After completing the above steps, print the value of the string S as the resultant time.

Below is the implementation of the above approach:

C++




// C++ program for the above approach
#include <iostream>
using namespace std;
// Function to find the maximum time
// by replacing '?' by any digits
void maxTime(string s)
{
    // Convert the string to the
    // character array
 
    // If the 0th index is '?'
    if (s[0] == '?') {
        if (s[1] <= '3' || s[1] == '?')
            s[0] = '2';
        else
            s[0] = '1';
    }
 
    // If the 1st index is '?'
    if (s[1] == '?') {
        if (s[0] != '2') {
            s[1] = 9;
        }
        else
            s[1] = 3;
    }
 
    // If the 3rd index is '?'
    if (s[3] == '?')
        s[3] = '5';
 
    // If the 4th index is '?'
    if (s[4] == '?')
        s[4] = '9';
 
    // Return new string
    cout << s << endl;
}
 
// Driver Code
int main()
{
    string S = "?4:5?";
    maxTime(S);
    return 0;
}
// This code is contributed by Potta Lokesh


Java




// Java program for the above approach
public class Main {
 
    // Function to find the maximum time
    // by replacing '?' by any digits
    public static void maxTime(String S)
    {
       
        // Convert the string to the
        // character array
        char[] s = S.toCharArray();
 
        // If the 0th index is '?'
        if (s[0] == '?') {
            if (s[1] <= '3' || s[1] == '?')
                s[0] = '2';
            else
                s[0] = '1';
        }
 
        // If the 1st index is '?'
        if (s[1] == '?') {
            if (s[0] != '2') {
                s[1] = 9;
            }
            else
                s[1] = 3;
        }
 
        // If the 3rd index is '?'
        if (s[3] == '?')
            s[3] = '5';
 
        // If the 4th index is '?'
        if (s[4] == '?')
            s[4] = '9';
 
        // Return new string
        System.out.println(
            new String(s));
    }
 
    // Driver Code
    public static void main(String[] args)
    {
        String S = "?4:5?";
        maxTime(S);
    }
}
 
// This code is contributed by lokeshpotta20.


Python3




# Python3 program for the above approach
 
# Function to find the maximum time
# by replacing '?' by any digits
def maxTime(s):
     
    # Convert the string to the
    # character array
 
    # If the 0th index is '?'
    s = list(s)
     
    if (s[0] == '?'):
        if (s[1] <= '3' or s[1] == '?'):
            s[0] = '2'
        else:
            s[0] = '1'
     
    # If the 1st index is '?'
    if (s[1] == '?'):
        if (s[0] != '2'):
            s[1] = 9
        else:
            s[1] = 3
 
    # If the 3rd index is '?'
    if (s[3] == '?'):
        s[3] = '5'
 
    # If the 4th index is '?'
    if (s[4] == '?'):
        s[4] = '9'
 
    # Return new string
    print("".join(s))
 
# Driver Code
S = "?4:5?"
maxTime(S)
 
# This code is contributed by _saurabh_jaiswal


C#




// C# program for the above approach
using System;
using System.Collections.Generic;
class gfg {
 
    // Function to find the maximum time
    // by replacing '?' by any digits
    public static void maxTime(String S)
    {
       
        // Convert the string to the
        // character array
        char[] s = S.ToCharArray();
 
        // If the 0th index is '?'
        if (s[0] == '?') {
            if (s[1] <= '3' || s[1] == '?')
                s[0] = '2';
            else
                s[0] = '1';
        }
 
        // If the 1st index is '?'
        if (s[1] == '?') {
            if (s[0] != '2') {
                s[1] = '9';
            }
            else
                s[1] = '3';
        }
 
        // If the 3rd index is '?'
        if (s[3] == '?')
            s[3] = '5';
 
        // If the 4th index is '?'
        if (s[4] == '?')
            s[4] = '9';
 
        // Return new string
        Console.Write(new String(s));
    }
 
    // Driver Code
    public static void Main(String[] args)
    {
        String S = "?4:5?";
        maxTime(S);
    }
}
 
// This code is contributed by shivanisinghss2110


Javascript




<script>
 
// Javascript program for the above approach
 
// Function to find the maximum time
// by replacing '?' by any digits
function maxTime(s)
{
 
    // Convert the string to the
    // character array
 
    // If the 0th index is '?'
    s = s.split("")
    if (s[0] == '?') {
        if (s[1] <= '3' || s[1] == '?')
            s[0] = '2';
        else
            s[0] = '1';
    }
 
    // If the 1st index is '?'
    if (s[1] == '?') {
        if (s[0] != '2') {
            s[1] = 9;
        }
        else
            s[1] = 3;
    }
 
    // If the 3rd index is '?'
    if (s[3] == '?')
        s[3] = '5';
 
    // If the 4th index is '?'
    if (s[4] == '?')
        s[4] = '9';
 
    // Return new string
    document.write(s.join(""));
}
 
// Driver Code
let S = "?4:5?";
maxTime(S);
 
// This code is contributed by gfgking
</script>


Output: 

14:59

 

Time Complexity: O(1)
Auxiliary Space: O(1)



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads