Skip to main content
Back to problems
#2437
Easy Algorithms

Number of valid clock times

String Enumeration
47.9% acceptance
Feb 25, 2026
306
246
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 m ust 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. *

Solution

Rust
Time O(n²)
Space O(1)
LeetCode
solution.rs
impl Solution {
  pub fn count_time(time: String) -> i32 {
    let t: Vec<char> = time.chars().collect();
    let mut count = 0;
    for h in 0..24i32 {
      for m in 0..60i32 {
        let h0 = char::from_digit((h / 10) as u32, 10).unwrap();
        let h1 = char::from_digit((h % 10) as u32, 10).unwrap();
        let m0 = char::from_digit((m / 10) as u32, 10).unwrap();
        let m1 = char::from_digit((m % 10) as u32, 10).unwrap();
        if (t[0] == '?' || t[0] == h0)
          && (t[1] == '?' || t[1] == h1)
          && (t[3] == '?' || t[3] == m0)
          && (t[4] == '?' || t[4] == m1)
        {
          count += 1;
        }
      }
    }
    count
  }
}