You are given a string time in the form of  hh:mm, where some of the digits in the string are hidden (represented by ?).

The valid times are those inclusively between 00:00 and 23:59.

Return the latest valid time you can get from time by replacing the hidden digits.

&nbsp;
Example 1:

Input: time = "2?:?0"
Output: "23:50"
Explanation: The latest hour beginning with the digit '2' is 23 and the latest minute ending with the digit '0' is 50.


Example 2:

Input: time = "0?:3?"
Output: "09:39"


Example 3:

Input: time = "1?:22"
Output: "19:22"


&nbsp;
Constraints:


	time is in the format hh:mm.
	It is guaranteed that you can produce a valid time from the given string.


class Solution:
def maximumTime(self, time: str) -> str:
    memo = {"0":"9",
            "1":"9",
            "?":"3", 
            "2":"3"}
    
    answer = ""
    for idx, val in enumerate(time):
        if val == "?":
            if idx == 0:
                if time[idx+1] == "?":
                    answer += "2"
                    
                else:
                    if int(time[idx+1]) >= 4:
                        answer += "1"
                
                    else: answer += "2"
                
            if idx == 1:
                answer += memo[time[idx-1]]
            
            if idx == 3:
                answer += "5"   
                
            if idx == 4:
                answer += "9"
        
        else:
            answer += val
    
    return answer
class Solution {
    public String maximumTime(String time) {
        char[] times = time.toCharArray();
        //for times[0]
        //if both characters of hour is ?, then hour is 23 then times[0] should be '2'("??:3?)".
        //if 2nd character of hour is <= 3, then hour can be in 20s  then times[0] should be '2'.
        //if 2nd character of hour is >3, then hour can only be in 10s  then times[0] should be '1'.
        if(times[0]=='?')
            times[0]= (times[1]<='3' || times[1]=='?') ? '2' : '1';
        //if 1st character of hour is 0 or 1, then hour can be 09 or 19 then times[1] should be '9'.
        //if 1st character of hour is 2, then hour can be 23 then times[1] should be '3'.
        if(times[1]=='?')
            times[1]= times[0]=='2' ? '3' : '9';
        //if both characters of minute is ? then minute is 59, or only 4th character is ? then 5_ so times[3] is always '5'.
        if(times[3]=='?')
            times[3]='5';
        //if 2nd character of minute is ?, then times[4] is '9'.
        if(times[4]=='?')
            times[4]='9';
        return new String(times);
    }
}
class Solution {
public:
    string maximumTime(string time) {
        //for time[0]
        //if both characters of hour is ?, then hour is 23 then time[0] should be '2'("??:3?)".
        //if 2nd character of hour is <= 3, then hour can be in 20s  then time[0] should be '2'.
        //if 2nd character of hour is >3, then hour can only be in 10s  then time[0] should be '1'.
        if(time[0]=='?')
            time[0]= (time[1]<='3' || time[1]=='?') ? '2' : '1';
        //if 1st character of hour is 0 or 1, then hour can be 09 or 19 then time[1] should be '9'.
        //if 1st character of hour is 2, then hour can be 23 then time[1] should be '3'.
        if(time[1]=='?')
            time[1]= time[0]=='2' ? '3' : '9';
        //if both characters of minute is ? then minute is 59, or only 4th character is ? then 5_ so time[3] is always '5'.
        if(time[3]=='?')
            time[3]='5';
        //if 2nd character of minute is ?, then time[4] is '9'.
        if(time[4]=='?')
            time[4]='9';
        return time;
    }
};
var maximumTime = function(time) {
    time = time.split('')
    if (time[0] === "?") time[0] = time[1] > 3 ? "1" : "2"
    if (time[1] === "?") time[1] = time[0] > 1 ? "3" : "9"
    if (time[3] === "?") time[3] = "5"
    if (time[4] === "?") time[4] = "9"
    return time.join('')
};
Latest Time by Replacing Hidden Digits