Number of Valid Clock Times - Problem

You are given a string time of length 5 representing the current time on a digital clock in the format "hh:mm". The earliest possible time is "00:00" and the latest possible time is "23:59".

In the string time, the digits represented by the ? symbol are unknown, and must be replaced with a digit from 0 to 9.

Return an integer answer, the number of valid clock times that can be created by replacing every ? with a digit from 0 to 9.

Input & Output

Example 1 — Single Unknown Digits
$ Input: time = "?4:5?"
Output: 20
💡 Note: First digit can be 0, 1, or 2 (since 34:5X would be invalid). Last digit can be 0-9. Total: 2 valid first digits × 10 last digits = 20 combinations.
Example 2 — Multiple Unknown Positions
$ Input: time = "0?:??"
Output: 60
💡 Note: First hour digit is 0, second can be 0-9 (10 choices). Minutes can be 00-59 (60 choices). But we need mm format, so it's 10 × 6 = 60.
Example 3 — All Digits Known
$ Input: time = "12:34"
Output: 1
💡 Note: No question marks, so only one valid time: 12:34 itself.

Constraints

  • time.length == 5
  • time is in the format "hh:mm"
  • Each character is either a digit or '?'

Visualization

Tap to expand
Valid Clock Times: Replace '?' with Valid DigitsInput?4:5?Constraints: Hours 00-23, Minutes 00-59First Position0, 12 choicesFixed Digits4 : 5givenLast Position0-910 choicesResult: 2 × 10 = 20 valid clock timesExamples: 04:50, 04:51, ..., 14:59
Understanding the Visualization
1
Input Analysis
Time string "?4:5?" with unknown digits marked as '?'
2
Apply Constraints
Valid hours: 00-23, Valid minutes: 00-59
3
Count Combinations
First '?' can be 0,1 (2 choices), last '?' can be 0-9 (10 choices)
Key Takeaway
🎯 Key Insight: Count valid possibilities for each position separately, then multiply - much faster than generating all combinations!
Asked in
Google 15 Facebook 12 Amazon 8
23.4K Views
Medium Frequency
~15 min Avg. Time
890 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