Created
March 13, 2016 05:08
-
-
Save jianminchen/16e9837b7b6b1e1a721c to your computer and use it in GitHub Desktop.
Game Throne - HackerRank string algorithm
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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