Maximize time by replacing ‘_’ in a given 24 Hour format time
Given a string S representing a time in 24 hours format, with ‘_’ placed at positions of some digits, the task is to find the maximum time possible by replacing the characters ‘_’ with any digit.
Examples:
Input: S = “0_:4_”
Output: 09:39
Explanation: Replacing the characters S[1] and S[4] with ‘9’ modifies the string to “09:49”, which is the maximum time possible.
Input: S = “__:__”
Output: 23:59
Approach: The given problem can be solved by greedily selecting the digits for each ‘_’ present in the string. Follow the steps below to solve the problem:
- If the character S[0] is equal to ‘_’ and S[1] is either ‘_’ or is less than 4, then assign ‘2‘ to S[0]. Otherwise, assign ‘1’ to S[0].
- If the character S[1] is equal to ‘_’ and S[0] is ‘2’, then assign ‘3’ to S[1]. Otherwise, assign ‘9‘ to S[1].
- If the character S[3] is equal to ‘_’, then assign ‘5’ to S[3].
- If the character S[4] is equal to ‘_’, then assign ‘9‘ to S[4].
- After completing the above steps, print the modified string S.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
string maximumTime(string s)
{
if (s[0] == '_' ) {
if ((s[1] == '_' )
|| (s[1] >= '0'
&& s[1] < '4' )) {
s[0] = '2' ;
}
else {
s[0] = '1' ;
}
}
if (s[1] == '_' ) {
if (s[0] == '2' ) {
s[1] = '3' ;
}
else {
s[1] = '9' ;
}
}
if (s[3] == '_' ) {
s[3] = '5' ;
}
if (s[4] == '_' ) {
s[4] = '9' ;
}
return s;
}
int main()
{
string S = "0_:4_" ;
cout << maximumTime(S);
return 0;
}
|
Java
class GFG{
static void maximumTime(String str)
{
char []s = str.toCharArray();
if (s[ 0 ] == '_' )
{
if ((s[ 1 ] == '_' ) ||
(s[ 1 ] >= '0' && s[ 1 ] < '4' ))
{
s[ 0 ] = '2' ;
}
else
{
s[ 0 ] = '1' ;
}
}
if (s[ 1 ] == '_' )
{
if (s[ 0 ] == '2' )
{
s[ 1 ] = '3' ;
}
else
{
s[ 1 ] = '9' ;
}
}
if (s[ 3 ] == '_' )
{
s[ 3 ] = '5' ;
}
if (s[ 4 ] == '_' )
{
s[ 4 ] = '9' ;
}
for ( int i = 0 ; i < s.length; i++)
System.out.print(s[i]);
}
static public void main (String []args)
{
String S = "0_:4_" ;
maximumTime(S);
}
}
|
Python3
def maximumTime(s):
s = list (s)
if (s[ 0 ] = = '_' ):
if ((s[ 1 ] = = '_' ) or (s[ 1 ] > = '0' and
s[ 1 ] < '4' )):
s[ 0 ] = '2'
else :
s[ 0 ] = '1'
if (s[ 1 ] = = '_' ):
if (s[ 0 ] = = '2' ):
s[ 1 ] = '3'
else :
s[ 1 ] = '9'
if (s[ 3 ] = = '_' ):
s[ 3 ] = '5'
if (s[ 4 ] = = '_' ):
s[ 4 ] = '9'
s = ''.join(s)
return s
if __name__ = = '__main__' :
S = "0_:4_"
print (maximumTime(S))
|
C#
using System;
class GFG{
static void maximumTime( string str)
{
char []s = str.ToCharArray();
if (s[0] == '_' )
{
if ((s[1] == '_' ) ||
(s[1] >= '0' && s[1] < '4' ))
{
s[0] = '2' ;
}
else
{
s[0] = '1' ;
}
}
if (s[1] == '_' )
{
if (s[0] == '2' )
{
s[1] = '3' ;
}
else
{
s[1] = '9' ;
}
}
if (s[3] == '_' )
{
s[3] = '5' ;
}
if (s[4] == '_' )
{
s[4] = '9' ;
}
for ( int i = 0; i < s.Length; i++)
Console.Write(s[i]);
}
static public void Main ()
{
string S = "0_:4_" ;
maximumTime(S);
}
}
|
Javascript
<script>
function maximumTime(str)
{
var s = str.split( "" );
if (s[0] == '_' )
{
if ((s[1] == '_' ) ||
(s[1] >= '0' && s[1] < '4' ))
{
s[0] = '2' ;
}
else
{
s[0] = '1' ;
}
}
if (s[1] == '_' )
{
if (s[0] == '2' )
{
s[1] = '3' ;
}
else
{
s[1] = '9' ;
}
}
if (s[3] == '_' )
{
s[3] = '5' ;
}
if (s[4] == '_' )
{
s[4] = '9' ;
}
for ( var i = 0; i < s.length; i++)
document.write(s[i]);
}
var S = "0_:4_" ;
maximumTime(S);
</script>
|
C
#include <stdio.h>
#include <string.h>
char * maximumTime( char s[])
{
if (s[0] == '_' ) {
if ((s[1] == '_' ) || (s[1] >= '0' && s[1] < '4' )) {
s[0] = '2' ;
}
else {
s[0] = '1' ;
}
}
if (s[1] == '_' ) {
if (s[0] == '2' ) {
s[1] = '3' ;
}
else {
s[1] = '9' ;
}
}
if (s[3] == '_' ) {
s[3] = '5' ;
}
if (s[4] == '_' ) {
s[4] = '9' ;
}
return s;
}
int main()
{
char S[] = "0_:4_" ;
printf ( "%s" , maximumTime(S));
return 0;
}
|
Time Complexity: O(1)
Auxiliary Space: O(1)
Last Updated :
09 Jun, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...