C++/Java/Python3 with picture (similar to 1371)
Intuition\nThis is similar to 1371. Find the Longest Substring Containing Vowels in Even Counts.\n\nWe can form a palindrome if the count of each included numbe
#### Intuition\nThis is similar to [1371. Find the Longest Substring Containing Vowels in Even Counts](https://leetcode.com/problems/find-the-longest-substring-containing-vowels-in-even-counts/discuss/534135/C%2B%2BJava-with-picture).\n\nWe can form a palindrome if the count of each included number (expcept maybe one) is even, and this is the property of an awesome string.\n\nA bit in our `mask` is `0` if the count for the corresponding number is even, and `1` if it\'s odd.  As we go through the string, we track odd/even counts in our mask. If we see the same `mask` again, the subarray between first poistion (exclusive) and the current position (inclusive) with the same `mask` has all numbers with the even count. \n\nWe will use the `dp` array to track the smallest (first) position of each `mask`. We can get the size of the substring by substracting it from the current poisition. \n\n> Note that position for zero mask is `-1`, as we need to include the very first character.\n\nNow, the difference from #1371 is that a palindrome can have up to one number with the odd count. Therefore, we need to also check all masks that are different from the current one by one bit. In other words, if two masks are different by one bit, that means that there is one odd count in the substring.\n\n![image](https://assets.leetcode.com/users/images/4fb7c48e-699d-4303-ade3-52300e87abf6_1596907985.4008424.png)\n\n**C++**\n```cpp\nint longestAwesome(string s) {\n    vector<int> dp(1024, s.size());\n    int res = 0, mask = 0;\n    dp[0] = -1;\n    for (auto i = 0; i < s.size(); ++i) {\n        mask ^= 1 << (s[i] - \'0\');\n        res = max(res, i - dp[mask]);\n        for (auto j = 0; j <= 9; ++j)\n            res = max(res, i - dp[mask ^ (1 << j)]);\n        dp[mask] = min(dp[mask], i);\n    }\n    return res;\n}\n```\n**Java**\n```java\npublic int longestAwesome(String s) {\n    int dp[] = new int[1024];\n    Arrays.fill(dp, s.length());\n    int res = 0, mask = 0;\n    dp[0] = -1;\n    for (int i = 0; i < s.length(); ++i) {\n        mask ^= 1 << (s.charAt(i) - \'0\');\n        res = Math.max(res, i - dp[mask]);\n        for (int j = 0; j <= 9; ++j)\n            res = Math.max(res, i - dp[mask ^ (1 << j)]);\n        dp[mask] = Math.min(dp[mask], i);\n    }\n    return res;\n}\n```\n\n**Python**\n```python\nclass Solution:\n    def longestAwesome(self, s: str) -> int:\n        mask, res = 0, 0\n        dp = [-1] + [len(s)] * 1023\n        for i in range(len(s)):\n            mask ^= 1 << (ord(s[i]) - 48)\n            for j in range(11):\n                check_mask = 1023 & (mask ^ (1 << j))\n                res = max(res, i - dp[check_mask])\n            dp[mask] = min(dp[mask], i)\n        return res\n```\n\n#### Complexity Analysis\n- Time: O(nk), where k is the number of unique characters we track. We go through the string once, and do k checks for each character.\n- Memory: O(2^ k), where k is the number of unique characters to track.
[]