[LeetCode]Longest Palindrome
很简单的题
统计一遍奇数个数字母和偶数个数字母
加起来减掉奇数个数字母的个数
然后如果存在奇数个数的字母就加一
public class Solution { public int LongestPalindrome(string s) { Dictionary<char, int> countChar = new Dictionary<char, int>(); foreach (var ch in s) { if (countChar.ContainsKey(ch)) countChar[ch]++; else countChar.Add(ch, 1); } int numOdd = 0; int countOdd = 0; int countEven = 0; int hasOdd = 0; foreach (var temp in countChar) { if (temp.Value % 2 == 0) { countEven += temp.Value; } else { numOdd++; countOdd += temp.Value; hasOdd = 1; } } return countEven + countOdd - numOdd + hasOdd; } }