Maximize the missing values in given time in HH:MM format
Last Updated :
23 Jul, 2021
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++
#include <iostream>
using namespace std;
void maxTime(string s)
{
if (s[0] == '?' ) {
if (s[1] <= '3' || s[1] == '?' )
s[0] = '2' ;
else
s[0] = '1' ;
}
if (s[1] == '?' ) {
if (s[0] != '2' ) {
s[1] = 9;
}
else
s[1] = 3;
}
if (s[3] == '?' )
s[3] = '5' ;
if (s[4] == '?' )
s[4] = '9' ;
cout << s << endl;
}
int main()
{
string S = "?4:5?" ;
maxTime(S);
return 0;
}
|
Java
public class Main {
public static void maxTime(String S)
{
char [] s = S.toCharArray();
if (s[ 0 ] == '?' ) {
if (s[ 1 ] <= '3' || s[ 1 ] == '?' )
s[ 0 ] = '2' ;
else
s[ 0 ] = '1' ;
}
if (s[ 1 ] == '?' ) {
if (s[ 0 ] != '2' ) {
s[ 1 ] = 9 ;
}
else
s[ 1 ] = 3 ;
}
if (s[ 3 ] == '?' )
s[ 3 ] = '5' ;
if (s[ 4 ] == '?' )
s[ 4 ] = '9' ;
System.out.println(
new String(s));
}
public static void main(String[] args)
{
String S = "?4:5?" ;
maxTime(S);
}
}
|
Python3
def maxTime(s):
s = list (s)
if (s[ 0 ] = = '?' ):
if (s[ 1 ] < = '3' or s[ 1 ] = = '?' ):
s[ 0 ] = '2'
else :
s[ 0 ] = '1'
if (s[ 1 ] = = '?' ):
if (s[ 0 ] ! = '2' ):
s[ 1 ] = 9
else :
s[ 1 ] = 3
if (s[ 3 ] = = '?' ):
s[ 3 ] = '5'
if (s[ 4 ] = = '?' ):
s[ 4 ] = '9'
print ("".join(s))
S = "?4:5?"
maxTime(S)
|
C#
using System;
using System.Collections.Generic;
class gfg {
public static void maxTime(String S)
{
char [] s = S.ToCharArray();
if (s[0] == '?' ) {
if (s[1] <= '3' || s[1] == '?' )
s[0] = '2' ;
else
s[0] = '1' ;
}
if (s[1] == '?' ) {
if (s[0] != '2' ) {
s[1] = '9' ;
}
else
s[1] = '3' ;
}
if (s[3] == '?' )
s[3] = '5' ;
if (s[4] == '?' )
s[4] = '9' ;
Console.Write( new String(s));
}
public static void Main(String[] args)
{
String S = "?4:5?" ;
maxTime(S);
}
}
|
Javascript
<script>
function maxTime(s)
{
s = s.split( "" )
if (s[0] == '?' ) {
if (s[1] <= '3' || s[1] == '?' )
s[0] = '2' ;
else
s[0] = '1' ;
}
if (s[1] == '?' ) {
if (s[0] != '2' ) {
s[1] = 9;
}
else
s[1] = 3;
}
if (s[3] == '?' )
s[3] = '5' ;
if (s[4] == '?' )
s[4] = '9' ;
document.write(s.join( "" ));
}
let S = "?4:5?" ;
maxTime(S);
</script>
|
Time Complexity: O(1)
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...