# Maximize time by replacing ‘_’ in a given 24 Hour format time

• Difficulty Level : Basic
• Last Updated : 03 May, 2021

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 and S 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 is equal to ‘_’ and S is either ‘_’ or is less than 4, then assign ‘2‘ to S. Otherwise, assign ‘1’ to S.
• If the character S is equal to ‘_’ and S is ‘2’, then assign ‘3’ to S. Otherwise, assign ‘9‘ to S.
• If the character S is equal to ‘_’, then assign ‘5’ to S.
• If the character S is equal to ‘_’, then assign ‘9‘ to S.
• 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 ` `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 == ``'_'``) {`   `        ``// If s is '_' or` `        ``// s is less than 4` `        ``if` `((s == ``'_'``)` `            ``|| (s >= ``'0'` `                ``&& s < ``'4'``)) {`   `            ``// Update s as 2` `            ``s = ``'2'``;` `        ``}`   `        ``// Otherwise, update s = 1` `        ``else` `{` `            ``s = ``'1'``;` `        ``}` `    ``}`   `    ``// If s is equal to '_'` `    ``if` `(s == ``'_'``) {`   `        ``// If s is equal to '2'` `        ``if` `(s == ``'2'``) {` `            ``s = ``'3'``;` `        ``}`   `        ``// Otherwise` `        ``else` `{` `            ``s = ``'9'``;` `        ``}` `    ``}`   `    ``// If S is equal to '_'` `    ``if` `(s == ``'_'``) {` `        ``s = ``'5'``;` `    ``}`   `    ``// If s is equal to '_'` `    ``if` `(s == ``'_'``) {` `        ``s = ``'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 is '_' or` `        ``// s is less than 4` `        ``if` `((s[``1``] == ``'_'``) || ` `            ``(s[``1``] >= ``'0'` `&& s[``1``] < ``'4'``)) ` `        ``{` `            `  `            ``// Update s as 2` `            ``s[``0``] = ``'2'``;` `        ``}`   `        ``// Otherwise, update s = 1` `        ``else` `        ``{` `            ``s[``0``] = ``'1'``;` `        ``}` `    ``}`   `    ``// If s is equal to '_'` `    ``if` `(s[``1``] == ``'_'``) ` `    ``{` `        `  `        ``// If s is equal to '2'` `        ``if` `(s[``0``] == ``'2'``)` `        ``{` `            ``s[``1``] = ``'3'``;` `        ``}`   `        ``// Otherwise` `        ``else` `        ``{` `            ``s[``1``] = ``'9'``;` `        ``}` `    ``}`   `    ``// If S is equal to '_'` `    ``if` `(s[``3``] == ``'_'``) ` `    ``{` `        ``s[``3``] = ``'5'``;` `    ``}`   `    ``// If s 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 is '_' or` `        ``# s is less than 4` `        ``if` `((s[``1``] ``=``=` `'_'``) ``or` `(s[``1``] >``=` `'0'` `and` `                              ``s[``1``] < ``'4'``)):` `                                  `  `            ``# Update s as 2` `            ``s[``0``] ``=` `'2'`   `        ``# Otherwise, update s = 1` `        ``else``:` `            ``s[``0``] ``=` `'1'`   `    ``# If s is equal to '_'` `    ``if` `(s[``1``] ``=``=` `'_'``):` `        `  `        ``# If s is equal to '2'` `        ``if` `(s[``0``] ``=``=` `'2'``):` `            ``s[``1``] ``=` `'3'`   `        ``# Otherwise` `        ``else``:` `            ``s[``1``] ``=` `'9'`   `    ``# If S is equal to '_'` `    ``if` `(s[``3``] ``=``=` `'_'``):` `        ``s[``3``] ``=` `'5'`   `    ``# If s 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 == ``'_'``) ` `    ``{` `        `  `        ``// If s is '_' or` `        ``// s is less than 4` `        ``if` `((s == ``'_'``) || ` `            ``(s >= ``'0'` `&& s < ``'4'``)) ` `        ``{` `            `  `            ``// Update s as 2` `            ``s = ``'2'``;` `        ``}`   `        ``// Otherwise, update s = 1` `        ``else` `        ``{` `            ``s = ``'1'``;` `        ``}` `    ``}`   `    ``// If s is equal to '_'` `    ``if` `(s == ``'_'``) ` `    ``{` `        `  `        ``// If s is equal to '2'` `        ``if` `(s == ``'2'``)` `        ``{` `            ``s = ``'3'``;` `        ``}`   `        ``// Otherwise` `        ``else` `        ``{` `            ``s = ``'9'``;` `        ``}` `    ``}`   `    ``// If S is equal to '_'` `    ``if` `(s == ``'_'``) ` `    ``{` `        ``s = ``'5'``;` `    ``}`   `    ``// If s is equal to '_'` `    ``if` `(s == ``'_'``)` `    ``{` `        ``s = ``'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

 ``

Output:

`09:49`

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

My Personal Notes arrow_drop_up
Recommended Articles
Page :