Hashmap + Prefix Bitmask | Clean Code with Explanation
Code\n\nclass Solution {\npublic:\n    int longestAwesome(string s) {\n        // hashmap to store the index of the first\n        // occurrence of a bitmask\n 
# Code\n```\nclass Solution {\npublic:\n    int longestAwesome(string s) {\n        // hashmap to store the index of the first\n        // occurrence of a bitmask\n        unordered_map<int,int> ind;\n\n        // the empty substring has no digits\n        // hence its bitmask will be 0\n        ind[0] = -1;\n\n        // A bitmask for keeping track of the occurrences of the digits.\n        // In order for a substring to be awesome, the digits have to occur even number of times and atmost 1\n        // digit can occur odd number of times. Whenever we see a digit in the substring, we toggle the bit\n        // corresponding to that digit in the bitmask, hence after we go through the entire substring\n        // If the ith bit is 1, then it means that the digit i occurs odd number of times (1, 3, 5, ....)\n        // If the ith bit is 0, then it means that the digit i occurs even number of times (0, 2, 4, ....).\n        // Therefore, for a substring to be awesome, the count of set bits in the bitmask has to be 0 or 1.\n        int mask = 0;\n\n        // the length of the longest awesome substring\n        int maxlen = 0;\n\n        // go through all the digits in the string\n        for(int i=0; i<s.size(); i++) {\n            // toggle the bit corresponding to the digit\n            mask ^= (1<<(s[i]-\'0\'));\n\n            // if we have seen this bitmask before, then it means that there\n            // is a substring with all even occurrences of digits.\n            if(ind.count(mask)) {\n                // find the length of the substring\n                maxlen = max(maxlen, i - ind[mask]);\n            }\n            // if we haven\'t seen this bitmask before\n            else {\n                // note the index of the first occurrence of the bitmask\n                ind[mask] = i;\n            }\n\n            // check for bitmasks that differ from the current bitmask by 1 bit.\n            for(int j=0; j<10; j++) {\n                if(ind.count(mask ^ (1<<j))) {\n                    // awesome substring with 1 odd occurrence of a digit\n                    maxlen = max(maxlen, i - ind[mask ^ (1<<j)]);\n                }\n            }\n        }\n\n        return maxlen;\n    }\n};\n```
['Hash Table', 'Bit Manipulation', 'C++']