-
Notifications
You must be signed in to change notification settings - Fork 693
/
Solution.cs
55 lines (49 loc) · 1.76 KB
/
Solution.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
43
44
45
46
47
48
49
50
51
52
53
54
55
/*
Problem: https://www.hackerrank.com/challenges/game-of-thrones/problem
C# Language Version: 6.0
.NET Framework Version: 4.7
Tool Version : Visual Studio Community 2017
Thoughts (Key points in algorithm) :
- Main idea is if the string is a palindrome then it falls in two cases:
- If length of input is odd, then except one character the count of every character appearing in the string should be an even number.
- If length of input is even, then the count of every character appearing in the string should be an even number.
Gotchas:
<None>
Time Complexity: O(n) //iterating the entire input is required.
Space Complexity: O(1) //No additional space required
*/
using System.Collections.Generic;
using System;
class Solution
{
static void Main(string[] args)
{
var inputLength = 0;
var charMap = new Dictionary<int, int>();
var nextChar = Console.Read();
while (nextChar != -1)
{
inputLength++;
if (charMap.ContainsKey(nextChar))
{
var currentCount = charMap[nextChar];
if (currentCount == 1)
charMap[nextChar] = 0;
else
charMap[nextChar] = 1;
}
else
charMap.Add(nextChar, 1);
nextChar = Console.Read();
}
var total = 0;
foreach (var item in charMap)
total += item.Value;
if (inputLength % 2 == 0 && total == 0)
Console.WriteLine("YES");
else if (inputLength % 2 != 0 && total == 1)
Console.WriteLine("YES");
else
Console.WriteLine("NO");
}
}