C++/Python, bitmask + prefix solution with explanation
To make a string be a palindrome using swaping,\nnumber of all of distinct element are even,\nor only one element are odd, and others are even.\n\nAnd use a way
To make a string be a palindrome using swaping,\nnumber of all of distinct element are even,\nor only one element are odd, and others are even.\n\nAnd use a way like prefix sum to record if frequency of each element in s[:cur+1] is odd or even, a bit of 1 is odd.\n\nodd - odd = odd, 1^1=0\neven - even = even, 0^0=0\nodd - even = even - odd = odd, 1^0=0^1=1\nWe can use xor operation to find parity of frequency of any substrings.\n\nif a substring is a palindrome, all frequency are even,\nor only one element are odd, and others are even.\n\ncurrent mask is x,\nmask of all frequency are even = 0,  x^?=0, ?=x.\nmask of only one element are odd, and others are even = 2^i, x^?=2^i, ?=(2^i) ^ x.\nwe can enumerate each a bit of 1 to find it,\nand use a hash map to record where the mask we first meet is, because the goal is to find the Longest.\n\nonly 2^10 type masks,\ntc is O(n), sc is O(2^10).\n\n### python\n```python\nclass Solution:\n    def longestAwesome(self, s: str) -> int:\n        masks = [-2] * 1024\n        masks[0] = -1\n        cur = 0\n        ans = 1\n        for i, x in enumerate(s):\n            cur ^= 1 << int(x)\n            if masks[cur] != -2: ans = max(ans, i - masks[cur])\n            for j in range(10):\n                if masks[cur ^ (1 << j)] != -2: ans = max(ans, i - masks[cur ^ (1 << j)])\n            if masks[cur] == -2: masks[cur] = i\n        return ans\n```\n\n### c++\n```cpp\nclass Solution {\npublic:\n    int longestAwesome(string s) {\n        vector<int> masks (1024, -2);\n        masks[0] = -1;\n        int cur = 0, ans = 1;\n        for (int i = 0; i < s.size(); i+=1) {\n            cur ^= 1 << (s[i] - \'0\');\n            if (masks[cur] != -2) ans = max(ans, i - masks[cur]);\n            for (int j = 0; j < 10; j+=1)\n                if (masks[cur ^ (1 << j)] != -2)\n                    ans = max(ans, i - masks[cur ^ (1 << j)]);\n            if (masks[cur] == -2) masks[cur] = i;\n        }\n        return ans;\n    }\n};\n```
['C', 'Bitmask', 'Python']