알고리즘 문제/Leetcode

1736. Latest Time by Replacing Hidden Digits

BEstyle 2022. 12. 2. 02:39

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.

class Solution:
    def maximumTime(self, time: str) -> str:
        ans=""
        #hour
        if time[0:2]=="??":
            ans+="23"
        elif time[0]=="?":
            if int(time[1])<4:
                ans+="2" + time[1]
            else:
                ans+="1" + time[1]
        elif time[1]=="?":
            if int(time[0])<2:
                ans+=time[0]+ "9"
            else:
                ans+=time[0] + "3"
        else:
            ans+=time[0:2]
        ans+=":"
        #minite
        if time[3:5]=="??":
            ans+="59"
        elif time[3]=="?":
            ans+="5" + time[4]
        elif time[4]=="?":
            ans+=time[3] + "9"
        else:
            ans+=time[3:5]
        return ans