coding
unsky
deepdim
thought

409.Longest Palindrome

  1. Longest Palindrome Add to List
    Description Submission Solutions
    Total Accepted: 28693
    Total Submissions: 64338
    Difficulty: Easy
    Contributors: Admin
    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.

很简单得题目

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
class Solution {
public:
int longestPalindrome(string s) {
unordered_map<char,int> ma;
for(int i=0;i<s.length();i++)
ma[s[i]]++;
unordered_map<char,int>::iterator it=ma.begin();
int len=0;
bool w=false;
for(;it!=ma.end();it++)
{len+=((it->second)/2);
if(it->second%2==1)
w=true;
}
return w?(len*2+1):(len*2);
}
};

坚持原创技术分享,您的支持将鼓励我继续创作!