Maximize the missing values in given time in HH : MM format | Java program for the above approach ; Function to find the maximum time by replacing ' ? ' by any digits ; Convert the string to the character array ; If the 0 th index is ' ? ' ; If the 1 st index is ' ? ' ; If the 3 rd index is ' ? ' ; If the 4 th index is ' ? ' ; Return new string ; Driver Code
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 ) ; } }