C++ || encode as 10bit integer || fast (187ms, 100%?)
Approach 1: encode as 10bit integer\n\nInstead of using a hashmap we can encode each two letter word into a 10bit integer. Just count them, and then see what\'s
### Approach 1: encode as 10bit integer\n\nInstead of using a hashmap we can encode each two letter word into a 10bit integer. Just count them, and then see what\'s the longest palindrom we can stich together with it.\n\n```cpp\n   static int longestPalindrome(const vector<string>& words) {\n        // Encode into an integer and count.\n        array<int, 1024> counts = {};\n        for (const string& word : words) {\n            const int code = ((word[0] - \'a\') << 5) | (word[1] - \'a\');\n            ++counts[code];\n        }\n        \n        int ans = 0;\n        \n        // Use symetric pieces.\n        bool odd = false;\n        for (int i = 0; i < 26; ++i) {\n            const int code = (i << 5) | i;\n            \n            if (counts[code] & 1) odd = true;\n            ans += counts[code] & ~1;\n            counts[code] = 0;\n        }\n        \n        // We can use one symetric piece in the center.\n        ans += odd;\n        \n        // Use all the other pieces if there is a mirror one.\n        for (int i = 0; i < size(counts); ++i) {\n            if (counts[i]) {\n                const int rev = ((i & 31) << 5) | (i >> 5);\n                ans += min(counts[i], counts[rev]) * 2;\n                counts[i] = 0;\n                counts[rev] = 0;\n            }\n        }\n        \n        return ans * 2;\n    }\n```\n\n**Complexity Analysis**\n  * Time Complexity: $$O(n)$$ we need to look at each of the input words, the rest is independent from the input size and linear.\n  * Space Complexity: $$O(1)$$, we need 1024 integers for ```counts```, but that doesn\'t change with the size of the input\n\n\n_As always: Feedback, questions, and comments are welcome. Leaving an up-vote sparks joy! :)_\n\n**p.s. Join us on the [LeetCode The Hard Way Discord Server](https://discord.gg/hFUyVyWy2E)!**
['Bit Manipulation', 'C']