Mutating counts without cleanup
Wrong move: Zero-count keys stay in map and break distinct/count constraints.
Usually fails on: Window/map size checks are consistently off by one.
Fix: Delete keys when count reaches zero.
Build confidence with an intuition-first walkthrough focused on hash map fundamentals.
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.
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 <= 10num consists of digits.Problem summary: 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.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Hash Map
"1210"
"030"
self-dividing-numbers)Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #2283: Check if Number Has Equal Digit Count and Digit Value
class Solution {
public boolean digitCount(String num) {
int[] cnt = new int[10];
int n = num.length();
for (int i = 0; i < n; ++i) {
++cnt[num.charAt(i) - '0'];
}
for (int i = 0; i < n; ++i) {
if (num.charAt(i) - '0' != cnt[i]) {
return false;
}
}
return true;
}
}
// Accepted solution for LeetCode #2283: Check if Number Has Equal Digit Count and Digit Value
func digitCount(num string) bool {
cnt := [10]int{}
for _, c := range num {
cnt[c-'0']++
}
for i, c := range num {
if int(c-'0') != cnt[i] {
return false
}
}
return true
}
# Accepted solution for LeetCode #2283: Check if Number Has Equal Digit Count and Digit Value
class Solution:
def digitCount(self, num: str) -> bool:
cnt = Counter(int(x) for x in num)
return all(cnt[i] == int(x) for i, x in enumerate(num))
// Accepted solution for LeetCode #2283: Check if Number Has Equal Digit Count and Digit Value
impl Solution {
pub fn digit_count(num: String) -> bool {
let mut cnt = vec![0; 10];
for c in num.chars() {
let x = c.to_digit(10).unwrap() as usize;
cnt[x] += 1;
}
for (i, c) in num.chars().enumerate() {
let x = c.to_digit(10).unwrap() as usize;
if cnt[i] != x {
return false;
}
}
true
}
}
// Accepted solution for LeetCode #2283: Check if Number Has Equal Digit Count and Digit Value
function digitCount(num: string): boolean {
const cnt: number[] = Array(10).fill(0);
for (const c of num) {
++cnt[+c];
}
for (let i = 0; i < num.length; ++i) {
if (cnt[i] !== +num[i]) {
return false;
}
}
return true;
}
Use this to step through a reusable interview workflow for this problem.
For each element, scan the rest of the array looking for a match. Two nested loops give n × (n−1)/2 comparisons = O(n²). No extra space since we only use loop indices.
One pass through the input, performing O(1) hash map lookups and insertions at each step. The hash map may store up to n entries in the worst case. This is the classic space-for-time tradeoff: O(n) extra memory eliminates an inner loop.
Review these before coding to avoid predictable interview regressions.
Wrong move: Zero-count keys stay in map and break distinct/count constraints.
Usually fails on: Window/map size checks are consistently off by one.
Fix: Delete keys when count reaches zero.