409. Longest Palindrome
1.问题描述
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.
2.C++
int longestPalindrome(string s) {
map<int, int> my_map;
map<int, int>::iterator it;
for(int i=0; i<s.size(); i++) {
it = my_map.find(s[i]);
if(it == my_map.end())
my_map[ s[ i ] ] = 1;
else
my_map[ s[ i ] ]++;
}
int flag = 0;
int num = 0;
for(it = my_map.begin(); it != my_map.end(); it++) {
if(it->second % 2 == 0)
num += it->second;
else {
flag = 1;
num += (it->second - 1);
}
}
return num + flag;
}
- 我的微信小程序
- 这是我的微信小程序扫一扫
-
- 我的微信公众号
- 我的微信公众号扫一扫
-
评论