# C#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.

Input:“abccccdd”

Output:7

Explanation:One longest palindrome that can be built is “dccaccd”, whose length is 7.

```public class Program {

public static void Main(string[] args) {
var s = "abccccdd";

var res = LongestPalindrome(s);
Console.WriteLine(res);

}

private static int LongestPalindrome(string s) {
var res = 0;
var odd = false;
var dic = new Dictionary<char, int>();
foreach(var c in s) {
if(dic.ContainsKey(c)) {
dic[c]++;
} else {
dic[c] = 1;
}
}

foreach(var item in dic) {
res += item.Value;
if(item.Value % 2 == 1) {
res -= 1;
odd = true;
}
}
return odd ? res + 1 : res;
}

}```

`7`

(2)