[LeetCode] Check if Number Has Equal Digit Count and Digit Value

2283. Check if Number Has Equal Digit Count and Digit Value

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.

1
2
3
4
5
6
7
8
9
10
11
12
13
class Solution {
public:
bool digitCount(string num) {
unordered_map<char, int> freq;
for(auto& ch : num) freq[ch]++;
for(int i = 0; i < num.length(); i++) {
char ch = i + '0';
int count = num[i] - '0';
if(freq[ch] != count) return false;
}
return true;
}
};
Author: Song Hayoung
Link: https://songhayoung.github.io/2022/05/29/PS/LeetCode/check-if-number-has-equal-digit-count-and-digit-value/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.