409. Longest Palindrome
Given a string which consists of lowercase or uppercase letters, find the length of the longest palindromes that can be built with those letters.
This is case sensitive, for example
"Aa"is not considered a palindrome here.
Assume the length of given string will not exceed 1,010.
Input:"abccccdd" Output:7 Explanation: One longest palindrome that can be built is "dccaccd", whose length is 7.
- Total len - number of odds + 1 (indicator of whether odds > 0)
class Solution(object): def longestPalindrome(self, s): odds = sum(v & 1 for v in collections.Counter(s).values()) return len(s) - odds + bool(odds)
Code: Python with counting even number (slower)
class Solution(object): def longestPalindrome(self, s): even = sum(v & ~1 for v in collections.Counter(s).values()) return even + (even < len(s))