Formatted question description: https://leetcode.ca/all/409.html

409. Longest Palindrome (Easy)

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.

Note:
Assume the length of given string will not exceed 1,010.

Example:

Input:
"abccccdd"

Output:
7

Explanation:
One longest palindrome that can be built is "dccaccd", whose length is 7.

Related Topics:
Hash Table

Similar Questions:

Solution 1.

// OJ: https://leetcode.com/problems/longest-palindrome/

// Time: O(N)
// Space: O(1) because there are at most 52 unique characters
class Solution {
public:
    int longestPalindrome(string s) {
        unordered_map<int, int> cnt;
        for (char c : s) cnt[c]++;
        int ans = 0, odd = 0;
        for (auto &p : cnt) {
            if (p.second % 2) {
                odd = 1; 
                ans += p.second - 1;
            } else ans += p.second;
        }
        return ans + odd;
    }
};

Java

  • class Solution {
        public int longestPalindrome(String s) {
            char[] array = s.toCharArray();
            int[] upperCounts = new int[26];
            int[] lowerCounts = new int[26];
            for (char c : array) {
                if (c <= 'Z')
                    upperCounts[c - 'A']++;
                else
                    lowerCounts[c - 'a']++;
            }
            boolean oddFlag = false;
            int count = 0;
            for (int i = 0; i < 26; i++) {
                int upperCount = upperCounts[i], lowerCount = lowerCounts[i];
                count += upperCount / 2 * 2;
                count += lowerCount / 2 * 2;
                if (upperCount % 2 != 0 || lowerCount % 2 != 0)
                    oddFlag = true;
            }
            if (oddFlag)
                count++;
            return count;
        }
    }
    
  • // OJ: https://leetcode.com/problems/longest-palindrome/
    // Time: O(N)
    // Space: O(1) because there are at most 52 unique characters
    class Solution {
    public:
        int longestPalindrome(string s) {
            unordered_map<int, int> cnt;
            for (char c : s) cnt[c]++;
            int ans = 0, odd = 0;
            for (auto &p : cnt) {
                if (p.second % 2) {
                    odd = 1; 
                    ans += p.second - 1;
                } else ans += p.second;
            }
            return ans + odd;
        }
    };
    
  • class Solution(object):
      def longestPalindrome(self, s):
        """
        :type s: str
        :rtype: int
        """
        maxLen = 0
        single = False
        d = {}
        for c in s:
          d[c] = d.get(c, 0) + 1
    
        for key in d:
          if d[key] >= 2:
            count = d[key]
            left = d[key] % 2
            d[key] = left
            maxLen += count - left
          if not single:
            if d[key] == 1:
              maxLen += 1
              single = True
        return maxLen
    
    

All Problems

All Solutions