Watch 10 video solutions for Check if Number Has Equal Digit Count and Digit Value, a easy level problem involving Hash Table, String, Counting. This walkthrough by Leetcode Hub has 1,390 views views. Want to try solving it yourself? Practice on FleetCode or read the detailed text solution.
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 Overview: You are given a numeric string num. For each index i, the digit at that position represents how many times digit i must appear in the entire string. The task is to check whether the actual frequency of every digit matches the value stored at its corresponding index.
Approach 1: Count Digits with a Frequency Array (O(n) time, O(1) space)
The direct solution counts how often each digit appears, then verifies whether those counts match the values in the string. Iterate once through num and update a frequency array of size 10 where freq[d] stores how many times digit d occurs. Then iterate again through the string: for index i, convert num[i] to an integer and compare it with freq[i]. If any index fails the check, return false. If all positions match, the number satisfies the condition. The array size is constant (digits 0–9), so the space complexity stays O(1). This approach relies on simple counting and works well because the digit range is fixed.
Approach 2: Count and Verify In-place (O(n^2) time, O(1) space)
This version avoids storing a separate frequency structure. For each index i, scan the entire string and count how many times digit i appears. Compare that count directly with num[i]. If any mismatch occurs, return false. Since each position triggers a full scan, the algorithm performs up to n scans of length n, giving O(n^2) time complexity. The advantage is minimal memory usage—no extra arrays or hash table structures. The logic is simple but slower for longer inputs.
Recommended for interviews: The frequency array solution is the expected answer. It demonstrates efficient use of string traversal and constant-size counting structures, achieving linear time with minimal space. Showing the naive repeated-count idea first can demonstrate problem understanding, but interviewers typically want the O(n) counting approach.
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Count Digits with Frequency Array | O(n) | O(1) | Best general solution; efficient for all inputs with constant digit range |
| Count and Verify In-place | O(n^2) | O(1) | Useful when avoiding extra memory or demonstrating brute-force reasoning |