leetcodedaybyday - Beats 100% with C++ and Beats 100% with Python3
IntuitionThe problem requires verifying if the frequency of each digit in the string matches the digit's value at the corresponding index. The task can be solve
# Intuition
The problem requires verifying if the frequency of each digit in the string matches the digit's value at the corresponding index. The task can be solved by counting the frequency of digits and comparing it with the expected values.

# Approach
1. **Count Frequencies**:
   - Use an array of size 10 to count the occurrences of each digit (0-9) in the given string.
   
2. **Validate Frequencies**:
   - Iterate over the string, and for each digit at index `i`, check if its frequency matches the value of the digit in the string at index `i`.
   
3. **Return Result**:
   - If all frequencies match the expected values, return `True`. Otherwise, return `False`.

# Complexity
- **Time Complexity**:  
  - \(O(n)\), where \(n\) is the length of the string `num`. We iterate over the string twice (once for counting frequencies and once for validation).
  
- **Space Complexity**:  
  - \(O(1)\), as the frequency array is of constant size (10).


# Code
```cpp []
class Solution {
public:
    bool digitCount(string num) {
        int n = num.size();
        vector<int> freq(10, 0);
        for (char c : num){
            freq[c - '0']++;
        }
        for (int i = 0; i < n; i++){
            if (freq[i] != (num[i] - '0')){
                return false;
            }
        }
        return true;
    }
};


```
```python3 []
class Solution:
    def digitCount(self, num: str) -> bool:
        n = len(num)
        freq = [0] * 10
        for c in num:
            freq[int(c)] += 1
        for i in range(n):
            if freq[i] != int(num[i]):
                return False
        return True



```


['C++', 'Python3']