Check if Number Has Equal Digit Count and Digit Value LeetCode solution in C++

1
class Solution {
2
public:
3
bool digitCount(string num) {
4
unordered_map<int, int> mpp;
5
int n = num.length();
6
for (auto it : num) {
7
int x = it - '0';
8
mpp[x]++; // Store the frequency of the char as a number
9
}
10
for (int i = 0; i < n; i++) {
11
int x = num[i] - '0'; // get the char as number
12
&nbsp;
13
&nbsp;
14
&nbsp;
15
&nbsp;
16
&nbsp;
17
&nbsp;
18
if (mpp[i] != x) // f the number is not equal to its frequency we return false
19
return false;
20
}
21
return true;
22
}
23
};

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0