LeetCode solutions

1858. Latest Time by Replacing Hidden Digits

My accepted Python solution to LeetCode problem 1858, Latest Time by Replacing Hidden Digits, running in 0ms.

  • Difficulty: Easy
  • Python
  • Runtime 0ms
  • Memory 17.5MB

Read the problem on LeetCode View on GitHub

Python

Accepted on LeetCode — runtime 0ms, memory 17.5MB, accepted 2025-12-31.

python
class Solution:
    def maximumTime(self, time: str) -> str:
        time = list(time)
        
        # First digit of hour
        if time[0] == '?':
            time[0] = '2' if time[1] == '?' or time[1] <= '3' else '1'
        
        # Second digit of hour
        if time[1] == '?':
            time[1] = '3' if time[0] == '2' else '9'
        
        # First digit of minute
        if time[3] == '?':
            time[3] = '5'
        
        # Second digit of minute
        if time[4] == '?':
            time[4] = '9'
        
        return ''.join(time)

Source