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.This problem asks us to determine if, for each digit position i
in a number represented as a string num
, the digit at that position (num[i]
) matches the number of times that digit i
appears in the entire string.
The most efficient approach involves two steps:
Counting Digit Occurrences: We iterate through the input string num
and count the occurrences of each digit (0-9). We can use an array or a hash map (dictionary in Python) for this purpose.
Comparing Counts to Digit Values: We iterate through the string num
again. For each digit at index i
, we check if the count of digit i
from step 1 matches the numerical value of the digit num[i]
. If a mismatch is found at any index, we immediately return false
. If all comparisons pass, we return true
.
Time Complexity: O(N), where N is the length of the input string num
. This is because we iterate through the string twice in the worst case.
Space Complexity: O(1). We use a fixed-size array (or hash map with a maximum of 10 entries) to store the digit counts. The space used doesn't depend on the input size, making it constant.
The code below demonstrates this approach in several programming languages. The core logic remains the same across all implementations, differing only in syntax and data structure choices.
from collections import Counter
class Solution:
def digitCount(self, num: str) -> bool:
# Count digit occurrences
counts = Counter(num)
# Compare counts to digit values
for i in range(len(num)):
digit = int(num[i])
if counts[str(i)] != digit:
return False
return True
class Solution {
public boolean digitCount(String num) {
int[] counts = new int[10];
for (char c : num.toCharArray()) {
counts[c - '0']++;
}
for (int i = 0; i < num.length(); i++) {
int digit = num.charAt(i) - '0';
if (counts[i] != digit) {
return false;
}
}
return true;
}
}
class Solution {
public:
bool digitCount(string num) {
vector<int> counts(10, 0);
for (char c : num) {
counts[c - '0']++;
}
for (int i = 0; i < num.length(); i++) {
int digit = num[i] - '0';
if (counts[i] != digit) {
return false;
}
}
return true;
}
};
/**
* @param {string} num
* @return {boolean}
*/
var digitCount = function(num) {
const counts = new Array(10).fill(0);
for (let c of num) {
counts[parseInt(c)]++;
}
for (let i = 0; i < num.length; i++) {
const digit = parseInt(num[i]);
if (counts[i] !== digit) {
return false;
}
}
return true;
};
func digitCount(num string) bool {
counts := make([]int, 10)
for _, c := range num {
counts[c-'0']++
}
for i := 0; i < len(num); i++ {
digit := int(num[i] - '0')
if counts[i] != digit {
return false
}
}
return true
}
public class Solution {
public bool DigitCount(string num) {
int[] counts = new int[10];
foreach (char c in num) {
counts[c - '0']++;
}
for (int i = 0; i < num.Length; i++) {
int digit = num[i] - '0';
if (counts[i] != digit) {
return false;
}
}
return true;
}
}
These code snippets all implement the same efficient algorithm, achieving a linear time complexity and constant space complexity. The choice of language mainly affects the syntax and specific data structures used (arrays or hash maps).