What are the odds? (Python & C++)
I count how many letters appear an odd number of times. Because we can use all letters, except for each odd-count letter we must leave one, except one of them w
I count how many letters appear an odd number of times. Because we can use **all** letters, except for each odd-count letter we must leave one, except one of them we can use.\n\nPython:\n\n    def longestPalindrome(self, s):\n        odds = sum(v & 1 for v in collections.Counter(s).values())\n        return len(s) - odds + bool(odds)\n\nC++:\n\n    int longestPalindrome(string s) {\n        int odds = 0;\n        for (char c='A'; c<='z'; c++)\n            odds += count(s.begin(), s.end(), c) & 1;\n        return s.size() - odds + (odds > 0);\n    }\n\nSimilar solutions (I actually like the `use` solutions better than the above, but I'm just so fond of my topic title :-)\n\n    def longestPalindrome(self, s):\n        use = sum(v & ~1 for v in collections.Counter(s).values())\n        return use + (use < len(s))\n\n    def longestPalindrome(self, s):\n        counts = collections.Counter(s).values()\n        return sum(v & ~1 for v in counts) + any(v & 1 for v in counts)\n\n    int longestPalindrome(string s) {\n        int use = 0;\n        for (char c='A'; c<='z'; c++)\n            use += count(s.begin(), s.end(), c) & ~1;\n        return use + (use < s.size());\n    }\n\n    int longestPalindrome(string s) {\n        vector<int> count(256);\n        for (char c : s)\n            ++count[c];\n        int odds = 0;\n        for (int c : count)\n            odds += c & 1;\n        return s.size() - odds + (odds > 0);\n    }\n\n    int longestPalindrome(string s) {\n        vector<int> count(256);\n        int odds = 0;\n        for (char c : s)\n            odds += ++count[c] & 1 ? 1 : -1;\n        return s.size() - odds + (odds > 0);\n    }
[]