Latest Time by Replacing Hidden Digits

Problem Id: 1736 Difficulty: Easy Tag: String Tag: Greedy


Intuition

Simply fill in digits from left to right. Consider the limitation of time by each digits.

Solution


class Solution:
    def maximumTime(self, time: str) -> str:
        time = list(time)
        if time[0] == '?':
            if time[1] in '456789':
                time[0] = '1'
            else:
                time[0] = '2'
        if time[1] == '?':
            if time[0] == '2':
                time[1] = '3'
            else:
                time[1] = '9'

        if time[3] == '?':
            time[3] = '5'
        if time[4] == '?':
            time[4] = '9'

        return ''.join(time)