website/content/ChapterFour/0400~0499/0409.Longest-Palindrome.md
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.
给定一个包含大写字母和小写字母的字符串,找到通过这些字母构造成的最长的回文串。在构造过程中,请注意区分大小写。比如 "Aa" 不能当做一个回文字符串。注意:假设字符串的长度不会超过 1010。
package leetcode
func longestPalindrome(s string) int {
counter := make(map[rune]int)
for _, r := range s {
counter[r]++
}
answer := 0
for _, v := range counter {
answer += v / 2 * 2
if answer%2 == 0 && v%2 == 1 {
answer++
}
}
return answer
}