Latest Time by Replacing Hidden Digits — LeetCode 1736 Python Solution
- Problem
- #1736
- Pattern
- Greedy
- Reading time
- 2 min
- Source
- leetcode.com
The problem
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.
Example
- 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.
Python solution
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)Complexity
| Measure | Complexity |
|---|---|
| Time | O(1) |
| Space | O(1) auxiliary |
Pattern: Greedy
Take the locally best option every time — when you can prove that never costs you later. LeetCode 1736. Latest Time by Replacing Hidden Digits is filed here on both counts: the reference solution below belongs to the algorithm family this hub collects, and LeetCode tags it Greedy.
The greedy guide has the Python template for the pattern and the 346 LeetCode problems that use it.
Related problems
Frequently asked questions
- How hard is LeetCode 1736. Latest Time by Replacing Hidden Digits?
- LeetCode 1736. Latest Time by Replacing Hidden Digits is rated Easy on LeetCode.
- What is the time complexity of LeetCode 1736. Latest Time by Replacing Hidden Digits?
- The Python solution on this page runs in O(1).
- What is the space complexity of LeetCode 1736. Latest Time by Replacing Hidden Digits?
- The Python solution on this page uses O(1) auxiliary space.
- What topics does LeetCode 1736. Latest Time by Replacing Hidden Digits cover?
- LeetCode 1736. Latest Time by Replacing Hidden Digits is tagged Greedy and String on LeetCode.