Number of Valid Clock Times - Problem

Imagine you're looking at a digital clock display, but some of the digits are broken and show up as question marks (?). Your task is to figure out how many different valid times this broken clock could be displaying!

You're given a string time of length 5 in the format "hh:mm", where:

  • The first two characters represent hours (00-23)
  • The third character is always a colon ':'
  • The last two characters represent minutes (00-59)
  • Some digits are replaced with ? symbols

Your goal: Count how many valid 24-hour format times can be formed by replacing each ? with any digit from 0 to 9.

Examples:

  • "?5:00" โ†’ Only "05:00", "15:00" are valid (not "25:00")
  • "0?:??" โ†’ Hours can be 00-09, minutes can be 00-59

Input & Output

example_1.py โ€” Simple hour wildcard
$ Input: time = "?5:00"
โ€บ Output: 2
๐Ÿ’ก Note: The first digit can be 0 (making 05:00) or 1 (making 15:00). It cannot be 2 or higher because 25:00 is not a valid time in 24-hour format.
example_2.py โ€” Multiple wildcards
$ Input: time = "0?:??"
โ€บ Output: 600
๐Ÿ’ก Note: First digit is fixed as 0, second digit can be 0-9 (giving hours 00-09), and minutes can be 00-59. So we have 10 ร— 60 = 600 valid combinations.
example_3.py โ€” Edge case with constraints
$ Input: time = "??:??"
โ€บ Output: 1440
๐Ÿ’ก Note: All positions are wildcards. Hours can be 00-23 (24 possibilities) and minutes can be 00-59 (60 possibilities). Total: 24 ร— 60 = 1440 valid times.

Visualization

Tap to expand
Smart Position Analysis: "?4:??"?Pos 04Pos 1:?Pos 3?Pos 4Hour[0] AnalysisSince Hour[1] = 4:Can be 0,1,2โœ“ 04:xx, 14:xxโœ— 24:xx invalidCount: 2Hour[1] AnalysisFixed as 4No choiceCount: 1Min[0] AnalysisMinutes: 00-59Can be 0-5Count: 6Min[1] AnalysisSecond digitCan be 0-9Count: 10Total Valid Times2 ร— 1 ร— 6 ร— 10 = 120
Understanding the Visualization
1
Analyze first hour digit
If ?, can be 0,1,2. If second hour digit is 0-3, first can be 0-2. If second is 4-9, first can only be 0-1.
2
Analyze second hour digit
If first digit is 0-1, second can be 0-9. If first digit is 2, second can only be 0-3.
3
Analyze minute digits
Both minute digits can be 0-9 independently (00-59 are all valid).
4
Multiply possibilities
Total valid times = (valid first hour) ร— (valid second hour) ร— (valid first minute) ร— (valid second minute)
Key Takeaway
๐ŸŽฏ Key Insight: Instead of generating all combinations, analyze constraints for each position independently and multiply the valid counts!

Time & Space Complexity

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

Where k is the number of question marks. We try all 10^k combinations.

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

Only using constant extra space for validation

n
2n
โœ“ Linear Space

Constraints

  • time.length == 5
  • time is in the format "hh:mm"
  • 0 โ‰ค hh โ‰ค 23
  • 0 โ‰ค mm โ‰ค 59
  • Some digits in time are replaced with ? symbol
Asked in
Google 35 Amazon 28 Meta 22 Microsoft 18
23.4K Views
Medium Frequency
~15 min Avg. Time
892 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