[C++, Java, Python] Two Pass Solution
Intuition\n Describe your first thoughts on how to solve this problem. \nThe task is to determine if a given string num satisfies the following condition:\n\nAt
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nThe task is to determine if a given string num satisfies the following condition:\n\nAt index i in the string, the value at that index (num[i]) represents the number of occurrences of the digit i in the entire string.\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n1. Count the frequency of each digit (0 to 9) that appears in the string.\n2. Verify the condition for each index:\n    - For index i, the value at that index (num[i]) should match the frequency of the digit i found in the entire string.\n    - If all indices match, return true; otherwise, return false.\n3. We will use:\n    - A dictionary (or map) to store the frequency of each digit.\n    - Two loops: One to count the occurrences, and another to verify the conditions.\n\n# Complexity\n- Time complexity: O(n)\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity: O(n)\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```cpp []\nclass Solution {\npublic:\n    bool digitCount(string num) {\n        map<int, int> values;\n\n        for(int i = 0; i < num.size(); i++){\n            int value = num[i] - \'0\';\n            values[value]++;\n        }\n\n        for(int i = 0; i < num.size(); i++){\n            if(values[i] != (num[i] - \'0\'))\n                return false;\n        }\n\n        return true;\n    }\n};\n```\n``` python3 []\nclass Solution:\n    def digitCount(self, num: str) -> bool:\n        values = defaultdict(int)\n\n        # Count the frequency of each digit in the string\n        for char in num:\n            value = int(char)\n            values[value] += 1\n\n        # Check if each index matches the frequency of the corresponding digit\n        for i in range(len(num)):\n            if values[i] != int(num[i]):\n                return False\n\n        return True\n```\n``` Java []\nclass Solution {\n    public boolean digitCount(String num) {\n        HashMap<Integer, Integer> values = new HashMap<>();\n\n        // Count the frequency of each digit in the string\n        for (int i = 0; i < num.length(); i++) {\n            int value = num.charAt(i) - \'0\';\n            values.put(value, values.getOrDefault(value, 0) + 1);\n        }\n\n        // Check if each index matches the frequency of the corresponding digit\n        for (int i = 0; i < num.length(); i++) {\n            if (values.getOrDefault(i, 0) != (num.charAt(i) - \'0\')) {\n                return false;\n            }\n        }\n\n        return true;\n    }\n}\n```\n
['Hash Table', 'String', 'Python', 'C++', 'Java', 'Python3']