Talentd
FleetCode
Make and follow sheets.Sign in

Check if Number Has Equal Digit Count and Digit Value solution and explanation

EasyCountingHash TableString4 min read73.1% acceptanceUpdated 9 Apr 2026
Leetcode 2283 - Check if Number Has Equal Digit Count and Digit Value || Biweekly Contest 79Leetcode Hub · 4:53 · 1K viewsWatch on YouTube
9 more videos on this problem
  1. Leetcode 2283 - Check if Number Has Equal Digit Count and Digit Value || Biweekly Contest 79Leetcode Hub · 4:53 · 1K views
  2. 2283. Check if Number Has Equal Digit Count and Digit Value (Leetcode Easy)Programming Live with Larry · 4:23 · 685 views
  3. LeetCode 2283 | Check if Number Has Equal Digit Count and Digit Value | Counting | JavaSleepy Cracker · 6:26 · 657 views
  4. LeetCode 2283 Explained: Check if Number Has Equal Digit Count & Value | Java O(n) SolutionRecurso · 13:54 · 515 views
  5. LeetCode Biweekly Contest 79 | Check if Number Has Equal Digit Count and Digit Value | PythonBayu Aji · 3:52 · 465 views
  6. Leetcode BiWeekly contest 79 Easy - Check if Number Has Equal Digit Count and Digit ValuePrakhar Agrawal · 3:42 · 410 views
  7. Check if Number Has Equal Digit Count and Digit Value - LeetCode 2283 - Java Solutionalgobest - Computer Science · 4:46 · 378 views
  8. LeetCode 2283 Solution in JavaCodes Cracked · 7:16 · 284 views
  9. Leetcode 2283 Check if Number Has Equal Digit Count and Digit ValueLETCODE · 1:52 · 277 views
  10. Check if Number Has Equal Digit Count and Digit Value | Leetcode biweekly contest 79(Approach)Rishabh Talwar Technical · 5:33 · 244 views

The problem

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.length
  • 1 <= n <= 10
  • num consists of digits.

Approach overview

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 1: Count Digits with a Frequency Array

Use an array to count the frequency of each digit (0-9) in the string. Check if each digit i matches the count specified at num[i].

def digitCount(num: str) -> bool:
    count = [0] * 10
    for c in num:
        count[int(c)] += 1
    return all(count[i] == int(num[i]) for i in range(len(num)))
public class Solution {
    public boolean digitCount(String num) {
        int[] count = new int[10];
        for (char c : num.toCharArray()) {
            count[c - '0']++;
        }
        for (int i = 0; i < num.length(); i++) {
            if (count[i] != num.charAt(i) - '0') {
                return false;
            }
        }
        return true;
    }
}
#include <vector>
#include <string>
using namespace std;

bool digitCount(const string& num) {
    vector<int> count(10, 0);
    for (char c : num) {
        count[c - '0']++;
    }
    for (int i = 0; i < num.length(); ++i) {
        if (count[i] != (num[i] - '0')) {
            return false;
        }
    }
    return true;
}
function digitCount(num) {
    let count = Array(10).fill(0);
    for (let c of num) {
        count[Number(c)]++;
    }
    for (let i = 0; i < num.length; i++) {
        if (count[i] !== Number(num[i])) {
            return false;
        }
    }
    return true;
}
#include <stdbool.h>
#include <string.h>

bool digitCount(const char* num) {
    int len = strlen(num);
    int count[10] = {0};
    for (int i = 0; i < len; ++i) {
        count[num[i] - '0']++;
    }
    for (int i = 0; i < len; ++i) {
        if (count[i] != (num[i] - '0')) {
            return false;
        }
    }
    return true;
}
public class Solution {
    public bool DigitCount(string num) {
        int[] count = new int[10];
        foreach (char c in num) {
            count[c - '0']++;
        }
        for (int i = 0; i < num.Length; i++) {
            if (count[i] != num[i] - '0') {
                return false;
            }
        }
        return true;
    }
}

The Python solution uses a list to track digit frequencies and validates each frequency against the corresponding index.

Complexity

Time Complexity: O(n).
Space Complexity: O(1).

Try this approach in the editor

Approach 2: Count and Verify In-place

Instead of storing results in an array, count digits as needed directly against the requirement in the input string.

def digitCount(num: str) -> bool:
    for i in range(len(num)):
        count = 0
        for c in num:
            if int(c) == i:
                count += 1
        if count != int(num[i]):
            return False
    return True
public class Solution {
    public boolean digitCount(String num) {
        for (int i = 0; i < num.length(); i++) {
            int count = 0;
            for (char c : num.toCharArray()) {
                if (c - '0' == i) {
                    count++;
                }
            }
            if (count != num.charAt(i) - '0') {
                return false;
            }
        }
        return true;
    }
}
#include <string>
using namespace std;

bool digitCount(const string& num) {
    for (int i = 0; i < num.length(); ++i) {
        int count = 0;
        for (char c : num) {
            if (c - '0' == i) {
                count++;
            }
        }
        if (count != (num[i] - '0')) {
            return false;
        }
    }
    return true;
}
function digitCount(num) {
    for (let i = 0; i < num.length; i++) {
        let count = 0;
        for (let c of num) {
            if (Number(c) === i) {
                count++;
            }
        }
        if (count !== Number(num[i])) {
            return false;
        }
    }
    return true;
}
#include <stdbool.h>
#include <string.h>

bool digitCount(const char* num) {
    int len = strlen(num);
    for (int i = 0; i < len; ++i) {
        int count = 0;
        for (int j = 0; j < len; ++j) {
            if (num[j] - '0' == i) {
                count++;
            }
        }
        if (count != (num[i] - '0')) {
            return false;
        }
    }
    return true;
}
public class Solution {
    public bool DigitCount(string num) {
        for (int i = 0; i < num.Length; i++) {
            int count = 0;
            foreach (char c in num) {
                if (c - '0' == i) {
                    count++;
                }
            }
            if (count != num[i] - '0') {
                return false;
            }
        }
        return true;
    }
}

The Python solution iteratively checks each digit's frequency inline using a nested loop structure.

Complexity

Time Complexity: O(n^2).
Space Complexity: O(1).

Try this approach in the editor

Approach 3: Counting + Enumeration

We can use an array cnt of length 10 to count the occurrences of each digit in the string num. Then, we enumerate each digit in the string num and check if its occurrence count equals the digit itself. If this condition is satisfied for all digits, we return true; otherwise, we return false.

The time complexity is O(n), and the space complexity is O(|\Sigma|). Here, n is the length of the string num, and |\Sigma| is the range of possible digit values, which is 10.

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;
}
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
}
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
    }
}
Try this approach in the editor

Complexity comparison

ApproachComplexity
Count Digits with a Frequency Array

Time Complexity: O(n).
Space Complexity: O(1).

Count and Verify In-place

Time Complexity: O(n^2).
Space Complexity: O(1).

Counting + Enumeration–

Complexity analysis

ApproachTimeSpaceWhen to Use
Count Digits with Frequency ArrayO(n)O(1)Best general solution; efficient for all inputs with constant digit range
Count and Verify In-placeO(n^2)O(1)Useful when avoiding extra memory or demonstrating brute-force reasoning

Frequently asked questions

Is Check if Number Has Equal Digit Count and Digit Value easy or hard?

The problem is classified as Easy. The main concept is digit frequency counting and index validation, which can be implemented with simple loops and a small array while maintaining O(n) time complexity.

Check if Number Has Equal Digit Count and Digit Value Python/Java solution

In Python or Java, iterate through the string and increment a frequency array indexed by digit value. After counting, loop through the string again and compare num[i] with freq[i]. If any mismatch occurs, return false; otherwise return true after the loop finishes.

How to solve Check if Number Has Equal Digit Count and Digit Value in O(n)?

Traverse the string once to build a frequency array for digits 0–9. Then iterate through each index i and check whether the integer value of num[i] equals the frequency of digit i. If every index matches, return true; otherwise return false. The algorithm performs two linear passes, giving O(n) time.

What is the best approach for Check if Number Has Equal Digit Count and Digit Value?

The most efficient approach uses a frequency array to count how often each digit appears. First iterate through the string and record digit frequencies, then compare each index i with the stored count of digit i. This runs in O(n) time with O(1) space because the digit range is fixed (0–9).

Is Check if Number Has Equal Digit Count and Digit Value asked at Google/Amazon/Meta?

Problems like this commonly appear in coding interviews focused on string processing and counting logic. While not always asked under the exact problem name, similar frequency validation questions appear in screening rounds at companies such as Amazon and Google.

What data structure is used in Check if Number Has Equal Digit Count and Digit Value?

The typical solution uses a fixed-size array (or occasionally a hash map) to store digit frequencies. Because digits range from 0 to 9, an integer array of size 10 is sufficient and provides constant-time updates and lookups.

What is the time complexity of Check if Number Has Equal Digit Count and Digit Value?

The optimal solution runs in O(n) time where n is the length of the numeric string. One pass counts digit frequencies and another pass verifies each index against the frequency array. Space complexity is O(1) because the counting structure has a constant size of 10.

Comments

Now write it yourselfClose this page and solve Check if Number Has Equal Digit Count and Digit Value in the editor, without looking.
Practise Check if Number Has Equal Digit Count and Digit Value