Welcome to Subscribe On Youtube

Formatted question description: https://leetcode.ca/all/1736.html

1736. Latest Time by Replacing Hidden Digits

Level

Easy

Description

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.

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”

Constraints:

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

Solution

First consider the hour. The hour must be in range [00, 23]. Replace the hidden digits with the maximum possible value in the range. Then consider the minute. The minute must be in range [00, 59]. The two digits of the minute should be replaced with the maximum possible digit, which are 5 and 9 respectively. Finally, return the replaced time.

  • class Solution {
        public String maximumTime(String time) {
            char[] array = time.toCharArray();
            if (array[0] == '?') {
                if (array[1] == '?') {
                    array[0] = '2';
                    array[1] = '3';
                } else {
                    if (array[1] <= '3')
                        array[0] = '2';
                    else
                        array[0] = '1';
                }
            }
            if (array[1] == '?') {
                if (array[0] == '2')
                    array[1] = '3';
                else
                    array[1] = '9';
            }
            if (array[3] == '?')
                array[3] = '5';
            if (array[4] == '?')
                array[4] = '9';
            return new String(array);
        }
    }
    
    ############
    
    class Solution {
        public String maximumTime(String time) {
            char[] t = time.toCharArray();
            if (t[0] == '?') {
                t[0] = t[1] >= '4' && t[1] <= '9' ? '1' : '2';
            }
            if (t[1] == '?') {
                t[1] = t[0] == '2' ? '3' : '9';
            }
            if (t[3] == '?') {
                t[3] = '5';
            }
            if (t[4] == '?') {
                t[4] = '9';
            }
            return new String(t);
        }
    }
    
  • // OJ: https://leetcode.com/problems/latest-time-by-replacing-hidden-digits/
    // Time: O(1)
    // Space: O(1)
    class Solution {
    public:
        string maximumTime(string time) {
            if (time[0] == '?') time[0] = time[1] == '?' || time[1] <= '3' ? '2' : '1';
            if (time[1] == '?') time[1] = time[0] == '2' ? '3' : '9';
            if (time[3] == '?') time[3] = '5';
            if (time[4] == '?') time[4] = '9';
            return time;
        }
    };
    
  • class Solution:
        def maximumTime(self, time: str) -> str:
            t = list(time)
            if t[0] == '?':
                t[0] = '1' if '4' <= t[1] <= '9' else '2'
            if t[1] == '?':
                t[1] = '3' if t[0] == '2' else '9'
            if t[3] == '?':
                t[3] = '5'
            if t[4] == '?':
                t[4] = '9'
            return ''.join(t)
    
    ############
    
    # 1736. Latest Time by Replacing Hidden Digits
    # https://leetcode.com/problems/latest-time-by-replacing-hidden-digits/
    
    class Solution:
        def maximumTime(self, time: str):
            time = list(time)
            
            if time[0] == "?":
                if time[1] == "?" or int(time[1]) <= 3:
                    time[0] = "2"
                else:
                    time[0] = "1"
            
            if time[1] == "?":
                if time[0] == "2":
                    time[1] = "3"
                elif time[0] == "1":
                    time[1] = "9"
                else:
                    time[1] = "9"
            
            if time[3] == "?":
                time[3] = "5"
            
            if time[4] == "?":
                time[4] = "9"
            
            return "".join(time)
                
    
  • func maximumTime(time string) string {
    	t := []byte(time)
    	if t[0] == '?' {
    		if t[1] >= '4' && t[1] <= '9' {
    			t[0] = '1'
    		} else {
    			t[0] = '2'
    		}
    	}
    	if t[1] == '?' {
    		if t[0] == '2' {
    			t[1] = '3'
    		} else {
    			t[1] = '9'
    		}
    	}
    	if t[3] == '?' {
    		t[3] = '5'
    	}
    	if t[4] == '?' {
    		t[4] = '9'
    	}
    	return string(t)
    }
    
  • /**
     * @param {string} time
     * @return {string}
     */
    var maximumTime = function (time) {
        const t = Array.from(time);
        if (t[0] === '?') {
            t[0] = t[1] >= '4' && t[1] <= '9' ? '1' : '2';
        }
        if (t[1] === '?') {
            t[1] = t[0] == '2' ? '3' : '9';
        }
        if (t[3] === '?') {
            t[3] = '5';
        }
        if (t[4] === '?') {
            t[4] = '9';
        }
        return t.join('');
    };
    
    

All Problems

All Solutions