Number of Valid Clock Times
EasyUpdated: Aug 2, 2025
Practice on:
Problem
You are given a string of length 5 called time, 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 integeranswer , the number of valid clock times that can be created by replacing every?_ with a digit from _0 to9.
Examples
Example 1
Input: time = "?5:00"
Output: 2
Explanation: We can replace the ? with either a 0 or 1, producing "05:00" or "15:00". Note that we cannot replace it with a 2, since the time "25:00" is invalid. In total, we have two choices.
Example 2
Input: time = "0?:0?"
Output: 100
Explanation: Each ? can be replaced by any digit from 0 to 9, so we have 100 total choices.
Example 3
Input: time = "??:??"
Output: 1440
Explanation: There are 24 possible choices for the hours, and 60 possible choices for the minutes. In total, we have 24 * 60 = 1440 choices.
Constraints
timeis a valid string of length5in the format"hh:mm"."00" <= hh <= "23""00" <= mm <= "59"- Some of the digits might be replaced with
'?'and need to be replaced with digits from0to9.
Solution
Method 1 – Brute Force Enumeration
Intuition
Since the number of possible times is small (24 hours × 60 minutes = 1440), we can try all possible hour and minute values, check if they match the pattern, and count the valid ones.
Approach
- For each hour from 0 to 23 and each minute from 0 to 59, format as "hh:mm".
- For each candidate, check if it matches the input pattern (where '?' matches any digit).
- Count all such valid times.
Code
C++
class Solution {
public:
int countTime(string time) {
int ans = 0;
for (int h = 0; h < 24; ++h) {
for (int m = 0; m < 60; ++m) {
char buf[6];
sprintf(buf, "%02d:%02d", h, m);
bool ok = true;
for (int i = 0; i < 5; ++i) {
if (time[i] != '?' && time[i] != buf[i]) ok = false;
}
if (ok) ++ans;
}
}
return ans;
}
};
Go
func countTime(time string) int {
ans := 0
for h := 0; h < 24; h++ {
for m := 0; m < 60; m++ {
t := fmt.Sprintf("%02d:%02d", h, m)
ok := true
for i := 0; i < 5; i++ {
if time[i] != '?' && time[i] != t[i] {
ok = false
}
}
if ok {
ans++
}
}
}
return ans
}
Java
class Solution {
public int countTime(String time) {
int ans = 0;
for (int h = 0; h < 24; h++) {
for (int m = 0; m < 60; m++) {
String t = String.format("%02d:%02d", h, m);
boolean ok = true;
for (int i = 0; i < 5; i++) {
if (time.charAt(i) != '?' && time.charAt(i) != t.charAt(i)) ok = false;
}
if (ok) ans++;
}
}
return ans;
}
}
Kotlin
class Solution {
fun countTime(time: String): Int {
var ans = 0
for (h in 0..23) {
for (m in 0..59) {
val t = "%02d:%02d".format(h, m)
var ok = true
for (i in 0..4) {
if (time[i] != '?' && time[i] != t[i]) ok = false
}
if (ok) ans++
}
}
return ans
}
}
Python
class Solution:
def countTime(self, time: str) -> int:
ans = 0
for h in range(24):
for m in range(60):
t = f"{h:02d}:{m:02d}"
if all(tc == '?' or tc == sc for tc, sc in zip(time, t)):
ans += 1
return ans
Rust
impl Solution {
pub fn count_time(time: String) -> i32 {
let mut ans = 0;
for h in 0..24 {
for m in 0..60 {
let t = format!("{:02}:{:02}", h, m);
if time.chars().zip(t.chars()).all(|(a,b)| a == '?' || a == b) {
ans += 1;
}
}
}
ans
}
}
TypeScript
class Solution {
countTime(time: string): number {
let ans = 0;
for (let h = 0; h < 24; h++) {
for (let m = 0; m < 60; m++) {
const t = `${h.toString().padStart(2, '0')}:${m.toString().padStart(2, '0')}`;
let ok = true;
for (let i = 0; i < 5; i++) {
if (time[i] !== '?' && time[i] !== t[i]) ok = false;
}
if (ok) ans++;
}
}
return ans;
}
}
Complexity
- ⏰ Time complexity:
O(1)(since we check at most 1440 times) - 🧺 Space complexity:
O(1)