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: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> |
14:59
Time Complexity: O(1)
Auxiliary Space: O(1)