Latest Time You Can Obtain After Replacing Characters - Problem

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 00 to 11
  • MM represents minutes from 00 to 59
  • The earliest time is 00:00 and the latest is 11: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

example_1.py โ€” Basic Case
$ Input: s = "?4:5?"
โ€บ Output: "04:59"
๐Ÿ’ก Note: For the first ?, we can choose 0 or 1 (since 2-9 would make hours > 11). We choose 0 to allow 4 in second position. For the last ?, we choose 9 to maximize minutes, giving us 04:59.
example_2.py โ€” Multiple Hour Options
$ Input: s = "?3:??"
โ€บ Output: "03:59"
๐Ÿ’ก Note: First digit must be 0 (since 1 would make 13 > 11). Minutes can be maximized to 59, giving us 03:59.
example_3.py โ€” All Question Marks
$ Input: s = "??:??"
โ€บ Output: "11:59"
๐Ÿ’ก Note: To get the latest time, we maximize hours to 11 (the maximum valid hour in 12-hour format) and minutes to 59, giving us 11:59.

Visualization

Tap to expand
Digital Clock Repair: Optimal StrategyInput: ?4:5??4:5?Step-by-step repair:Step 1: First ?Can be 0 or 1Choose 0 (since ?4 โ‰ค 11)Step 2: Second ?Minutes: max is 9Choose 9 โ†’ 59Result: 04:59Valid โœ“Latest possible โœ“Constraint Rules:Hour Constraints (HH):โ€ข If first digit is 0: second can be 0-9 โ†’ max 09โ€ข If first digit is 1: second can be 0-1 โ†’ max 11โ€ข If first digit is ?: choose 1 for potentially later timeMinute Constraints (MM):โ€ข First digit: 0-5 โ†’ choose 5 for maxโ€ข Second digit: 0-9 โ†’ choose 9 for maxโ€ข Maximum minutes: 59Time Complexity: O(1) - Only 4 positions to check!
Understanding the Visualization
1
Check First Hour Digit
If ?, choose 1 (to allow 11:XX) or 0 (to allow 09:XX). Choose 1 for later time.
2
Check Second Hour Digit
If first digit is 1, max is 1. If first digit is 0, max is 9. Choose accordingly.
3
Check First Minute Digit
Minutes range 00-59, so first digit can be at most 5.
4
Check Second Minute Digit
Second minute digit can always be 9 (since 59 is valid).
Key Takeaway
๐ŸŽฏ Key Insight: Instead of trying all combinations, we can determine the optimal digit for each position by considering the constraints of 12-hour format and choosing the maximum valid digit at each step.

Time & Space Complexity

Time Complexity
โฑ๏ธ
O(10^k)

Where k is the number of ? characters. In worst case k=4, so O(10^4) = O(10000) operations

n
2n
โœ“ Linear Growth
Space Complexity
O(1)

Only storing the current best time string and temporary variables

n
2n
โœ“ Linear Space

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
Asked in
Amazon 45 Google 32 Microsoft 28 Meta 21
42.3K Views
Medium Frequency
~12 min Avg. Time
1.5K Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen