Solution
Intuition\n Describe your first thoughts on how to solve this problem. \n\nBitmask + prefix. For a substring to be a palindrome, at most one number should appea
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\nBitmask + prefix. For a substring to be a palindrome, at most one number should appear an odd number of times. So we can maintain a bitmask where a set bit indicates that number appears an odd number of times so far. For every index, we set the mask and look for:\n\na) the first position the same mask appeared - corresponding to the substring in between having an even number of all numbers\n\nb) the first position each mask with 1 different bit to the current mask appeared corresponding to the substring in between having 1 number appearing an odd number of times (and all other numbers an even number of times)\n\nIf the prefix mask itself is 0 or (1 << i) for any digit i then the entire length of the string so far is can be rearranged into a palindrome.\n\nProblems which use the same idea:\n\nhttps://leetcode.com/problems/find-the-longest-substring-containing-vowels-in-even-counts/description/\n\nhttps://leetcode.com/problems/number-of-wonderful-substrings/description/\n\nKey steps:\n\n1. Maintain an array that stores the count or positions of all the masks to make calculations\n2. Identify the different masks that can be candidate solutions from a given mask\n3. Handle edge cases like for the 0 mask\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\nclass Solution {\npublic:\n    int longestAwesome(string s) {\n        // palindrome: at most one odd number of chars\n        vector<int> firstpos(1024,-1);\n\n        int mask = 0;\n        int alen = 0;\n\n        for(int i = 0; i<s.size(); i++)\n        {\n            int d = (s[i] - \'0\');\n            mask = mask ^ (1 << d);\n\n            if(firstpos[mask] == -1)\n            {\n                firstpos[mask] = i;\n            }\n\n            if(firstpos[mask] != -1)\n            {\n                if(mask == 0)\n                {\n                    alen = max(alen, i+1);\n                }\n                else\n                {\n                    alen = max(alen, i - firstpos[mask]);\n                }\n            }\n            \n            for(int j = 0; j<10; j++)\n            {\n                if(mask == (1 << j))\n                {\n                    alen = max(alen, i+1);\n                }\n\n                if(firstpos[mask ^ (1 << j)] != -1)\n                {\n                    alen = max(alen, i - firstpos[mask ^ (1 << j)]);\n                }\n            }\n        }\n\n        return alen;\n    }\n};\n```
['C++']