Confusing yet simple Hashmap Solution C++
This question was really easy, but head scratching at the same time. I had to waste so much time only because of the confusion to chose number or character type
This question was really easy, but head scratching at the same time. I had to waste so much time only because of the confusion to chose number or character type hashmap.\nSo, for simplicity we will maintain a int to int hashmap, which will store the frequency of a particular number.\nThen for every index, **we have to check if the frequency of that index in hashmap is equal to the number at that index**.\nHere is the solution too \uD83D\uDE0A\n```\nclass Solution {\npublic:\n    bool digitCount(string num) {\n        unordered_map<int,int> mpp;\n        int n= num.length();\n        for(auto it:num){\n            int x = it - \'0\';\n            mpp[x]++; // Store the frequency of the char as a number\n        }\n        for(int i=0;i<n;i++){\n            int x = num[i] - \'0\'; // get the char as number\n \xA0 \xA0 \xA0 \xA0 \xA0 \xA0if(mpp[i] != x) // f the number is not equal to its frequency we return false\n                return false;\n        }\n        return true;\n    }\n};\n\n```\n\n**Approach 2:**\nWe just count the occurences of that index in the string. Thanks to @VisD566 for this approach.\n\n```\nbool digitCount(string s) {\n    for(int i=0; i<s.size(); i++){\n        if(count(s.begin(), s.end(), i+\'0\') !=  s[i]-\'0\')\n            return false;\n    }\n    return true;\n}\n```
['String', 'C']