- 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.
很简单得题目12345678910111213141516171819class 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); }};