Check if Number Has Equal Digit Count and Digit Value
EasyUpdated: Jul 4, 2025
Practice on:
Problem
You are given a 0-indexed string num of length n consisting of digits.
Return true _if forevery index _i in the range0 <= i < n , the digiti occursnum[i]times innum , otherwise returnfalse.
Examples
Example 1
Input: num = "1210"
Output: true
Explanation:
num[0] = '1'. The digit 0 occurs once in num.
num[1] = '2'. The digit 1 occurs twice in num.
num[2] = '1'. The digit 2 occurs once in num.
num[3] = '0'. The digit 3 occurs zero times in num.
The condition holds true for every index in "1210", so return true.
Example 2
Input: num = "030"
Output: false
Explanation:
num[0] = '0'. The digit 0 should occur zero times, but actually occurs twice in num.
num[1] = '3'. The digit 1 should occur three times, but actually occurs zero times in num.
num[2] = '0'. The digit 2 occurs zero times in num.
The indices 0 and 1 both violate the condition, so return false.
Constraints
n == num.length1 <= n <= 10numconsists of digits.
Solution
Method 1 – Frequency Array
Intuition
We need to check if for every index i, the digit i appears exactly num[i] times in the string. This is a direct frequency counting problem.
Reasoning
By counting the frequency of each digit in the string, we can directly compare the count of digit i with the value at num[i] for all indices. If all match, the condition is satisfied.
Approach
- Initialize a frequency array of size 10 (for digits 0-9).
- Count the frequency of each digit in the string
num. - For each index
iinnum, check if the frequency of digitiequalsint(num[i]). - If all checks pass, return true; otherwise, return false.
Edge cases:
- Digits not present in the string should have a count of zero.
Code
C++
class Solution {
public:
bool digitCount(string num) {
vector<int> cnt(10);
for (char c : num) cnt[c - '0']++;
for (int i = 0; i < num.size(); ++i) {
if (cnt[i] != num[i] - '0') return false;
}
return true;
}
};
Go
func digitCount(num string) bool {
cnt := make([]int, 10)
for _, c := range num {
cnt[c-'0']++
}
for i := 0; i < len(num); i++ {
if cnt[i] != int(num[i]-'0') {
return false
}
}
return true
}
Java
class Solution {
public boolean digitCount(String num) {
int[] cnt = new int[10];
for (char c : num.toCharArray()) cnt[c - '0']++;
for (int i = 0; i < num.length(); i++) {
if (cnt[i] != num.charAt(i) - '0') return false;
}
return true;
}
}
Kotlin
class Solution {
fun digitCount(num: String): Boolean {
val cnt = IntArray(10)
for (c in num) cnt[c - '0']++
for (i in num.indices) {
if (cnt[i] != num[i] - '0') return false
}
return true
}
}
Python
class Solution:
def digit_count(self, num: str) -> bool:
cnt = [0] * 10
for c in num:
cnt[int(c)] += 1
for i, ch in enumerate(num):
if cnt[i] != int(ch):
return False
return True
Rust
impl Solution {
pub fn digit_count(num: String) -> bool {
let mut cnt = [0; 10];
for c in num.chars() {
cnt[c as usize - '0' as usize] += 1;
}
for (i, ch) in num.chars().enumerate() {
if cnt[i] != ch as usize - '0' as usize {
return false;
}
}
true
}
}
Complexity
- ⏰ Time complexity:
O(n), as we scan the string a constant number of times and n ≤ 10. - 🧺 Space complexity:
O(1), as the frequency array size is constant (10).