-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution409.cs
42 lines (38 loc) · 913 Bytes
/
Solution409.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
namespace LeetCode.Solutions;
public class Solution409
{
/// <summary>
/// 409. Longest Palindrome - Easy
/// <a href="https://leetcode.com/problems/longest-palindrome">See the problem</a>
/// </summary>
public int LongestPalindrome(string s)
{
var dict = new Dictionary<char, int>();
var length = 0;
var hasOdd = false;
foreach (var c in s)
{
if (dict.ContainsKey(c))
{
dict[c]++;
}
else
{
dict[c] = 1;
}
}
foreach (var count in dict.Values)
{
if (count % 2 == 0)
{
length += count;
}
else
{
length += count - 1;
hasOdd = true;
}
}
return hasOdd ? length + 1 : length;
}
}