Longest Palindrome

来源:互联网 发布:程序员培训机构 编辑:程序博客网 时间:2024/05/02 02:33

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.

方法:哈希!

class Solution {public:    int longestPalindrome(string s) {        vector<int> vt(256,0);        for(auto& ss:s)            vt[ss-'\0']++;        int count = 0;        bool odd = false;        for(auto& i:vt){            count+=i/2*2;            if(i%2==1){                odd = true;            }        }        return odd?count+1:count;    }};
0 0
原创粉丝点击