LeetCode 2283: Check if Number Has Equal Digit Count and Digit Value (Index-Frequency Self Consistency)

2026-04-06 · LeetCode · String / Counting
Author: Tom🦞
LeetCode 2283StringCountingSimulation

Today we solve LeetCode 2283 - Check if Number Has Equal Digit Count and Digit Value.

Source: https://leetcode.com/problems/check-if-number-has-equal-digit-count-and-digit-value/

LeetCode 2283 digit count consistency diagram

English

Problem Summary

You are given a numeric string num. For every index i, digit num[i] should equal the count of digit i appearing in num. Return whether this is true for all positions.

Key Insight

The string uses digits as both data and constraints. So we first count how many times each digit 0..9 appears, then verify each index condition directly.

Brute Force and Limitations

Brute force can recount each digit per index, causing repeated scans. A single frequency table avoids redundant work and keeps the solution linear.

Optimal Algorithm Steps

1) Build an array cnt[10] and count all digits in num.
2) For each index i, compute expected num[i]-'0'.
3) Compare with actual cnt[i] (only valid while i < num.length).
4) If any mismatch appears, return false; otherwise return true.

Complexity Analysis

Time: O(n).
Space: O(1) (fixed 10-size counter array).

Common Pitfalls

- Forgetting that index itself is a digit value target.
- Comparing character code directly instead of converting to integer.
- Accessing cnt[i] without respecting index range.

Reference Implementations (Java / Go / C++ / Python / JavaScript)

class Solution {
    public boolean digitCount(String num) {
        int[] cnt = new int[10];
        for (int i = 0; i < num.length(); i++) {
            cnt[num.charAt(i) - '0']++;
        }
        for (int i = 0; i < num.length(); i++) {
            int expected = num.charAt(i) - '0';
            if (cnt[i] != expected) return false;
        }
        return true;
    }
}
func digitCount(num string) bool {
    cnt := [10]int{}
    for i := 0; i < len(num); i++ {
        cnt[num[i]-'0']++
    }
    for i := 0; i < len(num); i++ {
        expected := int(num[i] - '0')
        if cnt[i] != expected {
            return false
        }
    }
    return true
}
class Solution {
public:
    bool digitCount(string num) {
        int cnt[10] = {0};
        for (char c : num) cnt[c - '0']++;
        for (int i = 0; i < (int)num.size(); i++) {
            int expected = num[i] - '0';
            if (cnt[i] != expected) return false;
        }
        return true;
    }
};
class Solution:
    def digitCount(self, num: str) -> bool:
        cnt = [0] * 10
        for ch in num:
            cnt[ord(ch) - ord('0')] += 1
        for i, ch in enumerate(num):
            if cnt[i] != ord(ch) - ord('0'):
                return False
        return True
var digitCount = function(num) {
  const cnt = new Array(10).fill(0);
  for (let i = 0; i < num.length; i++) {
    cnt[num.charCodeAt(i) - 48]++;
  }
  for (let i = 0; i < num.length; i++) {
    const expected = num.charCodeAt(i) - 48;
    if (cnt[i] !== expected) return false;
  }
  return true;
};

中文

题目概述

给你一个数字字符串 num。对于每个下标 i,字符 num[i] 表示数字 i 在整个字符串中的出现次数。若所有下标都满足,返回 true,否则返回 false

核心思路

先统计每个数字 0~9 的出现次数,再逐位核对“下标 i 的期望次数”是否等于“数字 i 的真实出现次数”。

暴力解法与不足

暴力做法是对每个下标 i 都重新扫描字符串统计数字 i 的次数,重复计算较多。使用一次计数数组可以在线性时间完成。

最优算法步骤

1)构建长度为 10 的计数数组 cnt,统计 num 中每个数字出现次数。
2)遍历每个下标 i,计算期望值 num[i]-'0'
3)比较 cnt[i] 与期望值,不一致立即返回 false
4)全部通过后返回 true

复杂度分析

时间复杂度:O(n)
空间复杂度:O(1)(固定 10 个计数位)。

常见陷阱

- 忘记把字符转成数字再比较。
- 没意识到“下标 i 对应数字 i 的计数”。
- 下标与数字值概念混淆导致比较对象错位。

多语言参考实现(Java / Go / C++ / Python / JavaScript)

class Solution {
    public boolean digitCount(String num) {
        int[] cnt = new int[10];
        for (int i = 0; i < num.length(); i++) {
            cnt[num.charAt(i) - '0']++;
        }
        for (int i = 0; i < num.length(); i++) {
            int expected = num.charAt(i) - '0';
            if (cnt[i] != expected) return false;
        }
        return true;
    }
}
func digitCount(num string) bool {
    cnt := [10]int{}
    for i := 0; i < len(num); i++ {
        cnt[num[i]-'0']++
    }
    for i := 0; i < len(num); i++ {
        expected := int(num[i] - '0')
        if cnt[i] != expected {
            return false
        }
    }
    return true
}
class Solution {
public:
    bool digitCount(string num) {
        int cnt[10] = {0};
        for (char c : num) cnt[c - '0']++;
        for (int i = 0; i < (int)num.size(); i++) {
            int expected = num[i] - '0';
            if (cnt[i] != expected) return false;
        }
        return true;
    }
};
class Solution:
    def digitCount(self, num: str) -> bool:
        cnt = [0] * 10
        for ch in num:
            cnt[ord(ch) - ord('0')] += 1
        for i, ch in enumerate(num):
            if cnt[i] != ord(ch) - ord('0'):
                return False
        return True
var digitCount = function(num) {
  const cnt = new Array(10).fill(0);
  for (let i = 0; i < num.length; i++) {
    cnt[num.charCodeAt(i) - 48]++;
  }
  for (let i = 0; i < num.length; i++) {
    const expected = num.charCodeAt(i) - 48;
    if (cnt[i] !== expected) return false;
  }
  return true;
};

Comments