Skip to main content
Back to problems
#2283
Easy Algorithms

Check if number has equal digit count and digit value

Hash Table String Counting
73.1% acceptance
Feb 25, 2026
676
101
You are given a 0-indexed string num of length n consisting of digits. Return true if for every index i in the range 0 <= i < n, the digit i occurs num[i] times in num, otherwise return false.

Solution

Rust
Time O(n)
Space O(1)
LeetCode
solution.rs
impl Solution {
  pub fn digit_count(num: String) -> bool {
    let bytes = num.as_bytes();
    let n = bytes.len();
    let mut count = [0u8; 10];
    for &b in bytes { count[(b - b'0') as usize] += 1; }
    for i in 0..n {
      let expected = (bytes[i] - b'0') as u8;
      if count[i] != expected { return false; }
    }
    true
  }
}