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.
下面是我的6ms C++ Solution,还在leetcode上分享了:
class Solution {
public:
int longestPalindrome(
string s) {
if(s.length() ==
0)
return 0;
int ans =
0;
unordered_map<char, int> umap;
for(
auto c : s)
umap[c]++;
for(
auto c : s){
ans += umap[c] /
2;
umap[c] =
0;
}
ans = ans *
2 +
1;
return ans > s.length() ? s.length() : ans;
}
};
转载请注明原文地址: https://ju.6miu.com/read-38580.html