Use bitmask of 10 letters as states
Approach\nSince there\'s only 10 possible letters/numbers, we can remember the state for each position, where the state can be defined as if the frequency of on
# Approach\nSince there\'s only 10 possible letters/numbers, we can remember the state for each position, where the state can be defined as if the frequency of one number is even or odd.\n\nAt any position, we can check if there\'s a previous position, who share the same state (same even/oddness for each number), if there is, we can use that position to form a palindrome, since excluding it will give us even number of each kind of letters.\n```\n[...a...] b\n\ndiff(a,b) = {x: 0, y: 1}\n            {x: 0}\n\n 0 1 2 3 4 5 6\n e o e o e o e  -- original state\n e o e o e o e  -- if the same state existed before, we can for a palindrome with no center (even palindrome)\n o o e o e o e  -- toggle one letter, try forming an odd palindrome with a center letter\n ^\n e e e o e o e  -- toggle one letter, try forming an odd palindrome with a center letter\n   ^\n```\n\n# Complexity\nSince we only have 10 letters (0-9), b = 10.\n\n- Time complexity: O(b*n)\n\n- Space complexity: O(2^(b+1))\n\n# Code\n```\nclass Solution {\npublic:\n    int longestAwesome(string s) {\n        vector<int> D(1<<11,-2), F(10, 0);\n        D[0] = -1;\n        int res = 1;\n        auto make_mask = [&]() {\n            int mask = 0;\n            for (int j = 0, b = 1; j <= 9; ++j, b<<=1) {\n                if (F[j]&1) mask += b;\n            }\n            return mask;\n        };\n\n        for (int i = 0; i < s.size(); ++i) {\n            ++F[s[i]-\'0\'];\n            int mask = make_mask();\n            for (int b = 1, j = 0; j <= 9; ++j, b <<= 1) {\n                if (F[j] <= 0) continue;\n                mask ^= b;\n                if (D[mask] >= -1) res = max(res, i-D[mask]);\n                mask ^= b;\n            }\n            if (D[mask] >= -1) res = max(res, i-D[mask]);\n            else D[mask] = i;\n        }\n        return res;\n    }\n};\n```
['C++']