Title
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.
题目大意:判断可以组成的回文最大长度,就是统计每个字符的字符,然后变化
Solution
//cpp:
class Solution {
public:
int longestPalindrome(string s) {
int len = 0;
int nums[256] = { 0 };
for (int i = 0; i < s.length(); i++) {
nums[s[i]]++;
}
bool flag = false;
for (int i = 0; i < 256; i++) {
len += (nums[i] - nums[i] % 2);
if (nums[i] % 2 == 1)
flag = true;
}
if (flag)
len++;
return len;
}
};