https://leetcode-cn.com/problems/longest-palindrome/
func longestPalindrome(_ s: String) -> Int {
//存储每个字符出现的次数
var dic = Dictionary<Character,Int>()
for char in s {
if dic.keys.contains(char) {
var charCount = dic[char]!
charCount = charCount + 1
dic.updateValue(charCount, forKey: char);
} else {
dic.updateValue(1, forKey: char);
}
}
//遍历key,count为偶数说明可以组成左右对称,奇数最后余1当做中心
var result = 0
for (_, cout) in dic {
if cout & 1 == 0 {//偶数
result = result + cout
}
if cout & 1 == 1 {//奇数
result = result + (cout - 1)
}
}
result = result < s.count ? result + 1 : result
return result
}