409. Longest Palindrome (Easy)

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.

Solution: Hash Table

class Solution {
public:
    int longestPalindrome(string s) {
        vector<int> v(52,0);
        for (auto& c : s) {
            if (c <= 'Z') v[c-'A']++;
            else v[c-'a'+26]++;
        }
        int res = 0; 
        bool mid = false;
        for (auto& i : v) {
            res += i;
            if (i % 2) {
                res--;
                mid = true;
            }
        }

        return res + mid;
    }
};

results matching ""

    No results matching ""