Check if Number Has Equal Digit Count and Digit Value

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
class Solution:
    def digitCount(self, num: str) -> bool:
        # Create a list to keep track of the occurrence of each digit
        occurrences = [0] * 10

        # Count the occurrences of each digit in the string num
        for digit in num:
            occurrences[int(digit)] += 1

        # Check if the condition holds for every index i
        for i in range(len(num)):
            if occurrences[i] != int(num[i]):
                return False

        return True

This code first counts the occurrences of each digit in the given string and then checks if the condition specified in the problem statement is met for each index i. If the condition holds for every index, it returns True, otherwise, it returns False.