-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path0409-Longest-palindrome.cs
More file actions
59 lines (50 loc) · 1.41 KB
/
0409-Longest-palindrome.cs
File metadata and controls
59 lines (50 loc) · 1.41 KB
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
using System;
using System.Collections.Generic;
using System.Text;
namespace Solution._0409.Longest_palindrome
{
public class _0409_Longest_palindrome
{
public int LongestPalindrome(string s)
{
HashSet<char> set = new HashSet<char>();
int count = 0;
foreach (char c in s)
{
if (!set.Add(c))
{
set.Remove(c);
count += 2;
}
}
return set.Count > 0 ? ++count : count;
// used more time.
//Dictionary<char, int> dic = new Dictionary<char, int>();
//int count = 0;
//foreach (char c in s)
//{
// if (dic.ContainsKey(c))
// dic[c]++;
// else
// dic.Add(c, 1);
//}
//bool odd = false;
//foreach (var pair in dic)
//{
// if (pair.Value >= 2)
// {
// if (pair.Value % 2 == 0)
// count += pair.Value;
// else
// {
// count += pair.Value - 1;
// odd = true;
// }
// }
// else odd = true;
//}
//if (odd) count++;
//return count;
}
}
}