Skip to content

Instantly share code, notes, and snippets.

@jianminchen
Created March 13, 2016 05:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jianminchen/16e9837b7b6b1e1a721c to your computer and use it in GitHub Desktop.
Save jianminchen/16e9837b7b6b1e1a721c to your computer and use it in GitHub Desktop.
Game Throne - HackerRank string algorithm
using System;
using System.Linq;
using System.Text.RegularExpressions;
using System.Collections.Generic;
using System.IO;
class Solution {
static void Main(String[] args) {
// input is a single line which will contain the input string
var input = Console.ReadLine();
// 1<=length of string <= 10^5
if (input == null || input.Length < 1 || input.Length > 100000)
{
Console.WriteLine("NO");
return;
}
// each character of the string is a lowercase english alphabet
if (!Regex.IsMatch(input, "^[a-z]*$"))
{
Console.WriteLine("NO");
return;
}
// figure out how many times each character appears
var charCount = new Dictionary<char, int>();
foreach (var c in input)
{
if (!charCount.ContainsKey(c))
{
charCount[c] = 0;
}
charCount[c]++;
}
// palindrome exists if there is at most 1 character that appears an odd number of times
if (charCount.Count(x => x.Value % 2 == 1) > 1)
{
Console.WriteLine("NO");
}
else
{
Console.WriteLine("YES");
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment