Leetcode 2283 Check if Number Has Equal Digit Count and Digit Value in c++ | Hindi Coding Community

0

 


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.




class Solution {
public:
bool digitCount(string num) {
int arr[10] = {0};
for(auto it : num){
arr[it-'0']++;
}
for(int i=0;i<num.size();i++){
if(arr[i]!=(num[i])-'0') return false;
}
return true;
}
};


Post a Comment

0Comments
Post a Comment (0)

#buttons=(Accept !) #days=(20)

Our website uses cookies to enhance your experience. Learn More
Accept !