Imagine you have a broken digital clock that displays a 12-hour format time where some digits are corrupted and show up as ? characters. Your goal is to fix this clock by replacing all the ? marks with actual digits to create the latest possible valid time.
The time format is HH:MM where:
- HH represents hours from
00to11 - MM represents minutes from
00to59 - The earliest time is
00:00and the latest is11:59
For example, if you see "?4:5?", you want to make it as late as possible. The hour could be 04 (since 14 would be invalid), and the minute could be 59, giving us "04:59".
Goal: Transform the corrupted time string into the latest possible valid 12-hour format time.
Input & Output
Visualization
Time & Space Complexity
Where k is the number of ? characters. In worst case k=4, so O(10^4) = O(10000) operations
Only storing the current best time string and temporary variables
Constraints
-
s.length == 5 -
s is in the format
"HH:MM" -
Each character is either a digit or
'?' - Hours range from 00 to 11 (12-hour format)
- Minutes range from 00 to 59