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
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.
โ Linear Growth
Space Complexity
O(1)
Only using constant extra space for validation
โ 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
๐ก
Explanation
AI Ready
๐ก Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code