【leetcode】409. Longest Palindrome
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 class Solution { 2 public: 3 int longestPalindrome(string s) { 4 int arr[52]={0}; 5 for(int i=0;i<s.size();i++) 6 { 7 if(s[i]>='A'&&s[i]<='Z') 8 arr[s[i]-'A'+26]++; 9 else 10 arr[s[i]-'a']++; 11 } 12 int ret=0; 13 int hasOdd=0; 14 for(int i=0;i<52;i++) 15 { 16 if(arr[i]%2==0) 17 ret+=arr[i]; 18 else 19 { 20 ret+=arr[i]-1; 21 hasOdd=1; 22 } 23 } 24 return ret+hasOdd; 25 } 26 };