Maximize time by replacing '_' in a given 24 Hour format time
Last Updated :
09 Jun, 2022
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++
// C++ program for the above approach
#include <bits/stdc++.h>
using namespace std;
// Function to find the maximum
// time possible by replacing
// each '_' with any digit
string maximumTime(string s)
{
// If the first character is '_'
if (s[0] == '_') {
// If s[1] is '_' or
// s[1] is less than 4
if ((s[1] == '_')
|| (s[1] >= '0'
&& s[1] < '4')) {
// Update s[0] as 2
s[0] = '2';
}
// Otherwise, update s[0] = 1
else {
s[0] = '1';
}
}
// If s[1] is equal to '_'
if (s[1] == '_') {
// If s[0] is equal to '2'
if (s[0] == '2') {
s[1] = '3';
}
// Otherwise
else {
s[1] = '9';
}
}
// If S[3] is equal to '_'
if (s[3] == '_') {
s[3] = '5';
}
// If s[4] is equal to '_'
if (s[4] == '_') {
s[4] = '9';
}
// Return the modified string
return s;
}
// Driver Code
int main()
{
string S = "0_:4_";
cout << maximumTime(S);
return 0;
}
Java
// Java program for the above approach
class GFG{
// Function to find the maximum
// time possible by replacing
// each '_' with any digit
static void maximumTime(String str)
{
char []s = str.toCharArray();
// If the first character is '_'
if (s[0] == '_')
{
// If s[1] is '_' or
// s[1] is less than 4
if ((s[1] == '_') ||
(s[1] >= '0' && s[1] < '4'))
{
// Update s[0] as 2
s[0] = '2';
}
// Otherwise, update s[0] = 1
else
{
s[0] = '1';
}
}
// If s[1] is equal to '_'
if (s[1] == '_')
{
// If s[0] is equal to '2'
if (s[0] == '2')
{
s[1] = '3';
}
// Otherwise
else
{
s[1] = '9';
}
}
// If S[3] is equal to '_'
if (s[3] == '_')
{
s[3] = '5';
}
// If s[4] is equal to '_'
if (s[4] == '_')
{
s[4] = '9';
}
// Print the modified string
for(int i = 0; i < s.length; i++)
System.out.print(s[i]);
}
// Driver Code
static public void main (String []args)
{
String S = "0_:4_";
maximumTime(S);
}
}
// This code is contributed by AnkThon
Python3
# Python3 program for the above approach
# Function to find the maximum
# time possible by replacing
# each '_' with any digit
def maximumTime(s):
s = list(s)
# If the first character is '_'
if (s[0] == '_'):
# If s[1] is '_' or
# s[1] is less than 4
if ((s[1] == '_') or (s[1] >= '0' and
s[1] < '4')):
# Update s[0] as 2
s[0] = '2'
# Otherwise, update s[0] = 1
else:
s[0] = '1'
# If s[1] is equal to '_'
if (s[1] == '_'):
# If s[0] is equal to '2'
if (s[0] == '2'):
s[1] = '3'
# Otherwise
else:
s[1] = '9'
# If S[3] is equal to '_'
if (s[3] == '_'):
s[3] = '5'
# If s[4] is equal to '_'
if (s[4] == '_'):
s[4] = '9'
# Return the modified string
s = ''.join(s)
return s
# Driver Code
if __name__ == '__main__':
S = "0_:4_"
print(maximumTime(S))
# This code is contributed by ipg2016107
C#
// C# program for the above approach
using System;
class GFG{
// Function to find the maximum
// time possible by replacing
// each '_' with any digit
static void maximumTime(string str)
{
char []s = str.ToCharArray();
// If the first character is '_'
if (s[0] == '_')
{
// If s[1] is '_' or
// s[1] is less than 4
if ((s[1] == '_') ||
(s[1] >= '0' && s[1] < '4'))
{
// Update s[0] as 2
s[0] = '2';
}
// Otherwise, update s[0] = 1
else
{
s[0] = '1';
}
}
// If s[1] is equal to '_'
if (s[1] == '_')
{
// If s[0] is equal to '2'
if (s[0] == '2')
{
s[1] = '3';
}
// Otherwise
else
{
s[1] = '9';
}
}
// If S[3] is equal to '_'
if (s[3] == '_')
{
s[3] = '5';
}
// If s[4] is equal to '_'
if (s[4] == '_')
{
s[4] = '9';
}
// Print the modified string
for(int i = 0; i < s.Length; i++)
Console.Write(s[i]);
}
// Driver Code
static public void Main ()
{
string S = "0_:4_";
maximumTime(S);
}
}
// This code is contributed by AnkThon
JavaScript
<script>
// javascript program for the above approach
// Function to find the maximum
// time possible by replacing
// each '_' with any digit
function maximumTime(str)
{
var s = str.split("");
// If the first character is '_'
if (s[0] == '_')
{
// If s[1] is '_' or
// s[1] is less than 4
if ((s[1] == '_') ||
(s[1] >= '0' && s[1] < '4'))
{
// Update s[0] as 2
s[0] = '2';
}
// Otherwise, update s[0] = 1
else
{
s[0] = '1';
}
}
// If s[1] is equal to '_'
if (s[1] == '_')
{
// If s[0] is equal to '2'
if (s[0] == '2')
{
s[1] = '3';
}
// Otherwise
else
{
s[1] = '9';
}
}
// If S[3] is equal to '_'
if (s[3] == '_')
{
s[3] = '5';
}
// If s[4] is equal to '_'
if (s[4] == '_')
{
s[4] = '9';
}
// Print the modified string
for(var i = 0; i < s.length; i++)
document.write(s[i]);
}
// Driver Code
var S = "0_:4_";
maximumTime(S);
// This code is contributed by bunnyram19.
</script>
C
// C program for above approach
#include <stdio.h>
#include <string.h>
// Function to find the maximum
// time possible by replacing
// each '_' with any digit
char* maximumTime(char s[])
{
// If the first character is '_'
if (s[0] == '_') {
// If s[1] is '_' or
// s[1] is less than 4
if ((s[1] == '_') || (s[1] >= '0' && s[1] < '4')) {
// Update s[0] as 2
s[0] = '2';
}
else { // Otherwise, update s[0] = 1
s[0] = '1';
}
}
// If s[1] is equal to '_'
if (s[1] == '_') {
// If s[0] is equal to '2'
if (s[0] == '2') {
s[1] = '3';
}
// otherwise
else {
s[1] = '9';
}
}
// If S[3] is equal to '_'
if (s[3] == '_') {
s[3] = '5';
}
// If s[4] is equal to '_'
if (s[4] == '_') {
s[4] = '9';
}
return s; // Return the modified string
}
int main()
{
char S[] = "0_:4_";
printf("%s", maximumTime(S));
return 0;
}
// This code is contributed by Tapesh (tapeshdua420)
Time Complexity: O(1)
Auxiliary Space: O(1)
Similar Reads
Maximize the missing values in given time in HH:MM format 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:59Explanation:After replacing the f
6 min read
Maximum time in HH:MM:SS format that can be represented by given six digits Given an array arr[] consisting of six integer digits only, the task is to return the maximum time in a 24-hour format that can be represented using the digits from the given array. Note: The minimum time in 24-hour format is 00:00:00, and the maximum is 23:59:59. If a valid time cannot be formed, t
9 min read
Time Difference between given times in HH:MM:SS format Given 2 times 'st' and 'et' in HH: MM: SS format. The task is to print the time difference between st and et in HH:MM: SS format Examples: Input: st = 13:50:45, et = 14:55:50Output: 01:05:05Explanation: The time gap is 1 hour 5 minutes and 5 seconds. Input: st = 12:00:00, et = 24:00:00Output: 12:00:
6 min read
Maximum time such that absolute difference between hour and minute lies in given range Given a 24-hour time value, where on some numbers are question marks( '?' ), and two integers L and R. The question mark can be replaced with any number. The task is to find the maximum time such that the absolute difference between hour value and minute value lies in the interval [L, R]. If no poss
13 min read
Program to find the time after K minutes from given time You are given a time T in 24-hour format (hh:mm) and a positive integer K, you have to tell the time after K minutes in 24-hour time.Examples: Input: T = 12:43, K = 21 Output: 13:04 Input: T = 20:39, K = 534 Output: 05:33 Approach: Convert the given time in minutesAdd K to it let it be equal to M.Co
6 min read
Maximum possible time that can be formed from four digits Given an array arr[] having 4 integer digits only. The task is to return the maximum 24 hour time that can be formed using the digits from the array. Note that the minimum time in 24 hour format is 00:00, and the maximum is 23:59. If a valid time cannot be formed then return -1. Examples: Input: arr
12 min read