Created
March 13, 2016 04:50
-
-
Save jianminchen/e7038fa7e59c96378675 to your computer and use it in GitHub Desktop.
HackerRank: string - Game Throne https://www.hackerrank.com/challenges/game-of-thrones
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.Collections.Generic; | |
using System.Linq; | |
using System.Text; | |
using System.Threading.Tasks; | |
namespace GameOfThrones | |
{ | |
class Program | |
{ | |
/* | |
* GameOfThrones | |
* https://www.hackerrank.com/challenges/game-of-thrones | |
* | |
* | |
*/ | |
static void Main(string[] args) | |
{ | |
string s = Console.ReadLine(); | |
if (canBePalindrome(s)) | |
Console.WriteLine("YES"); | |
else | |
Console.WriteLine("NO"); | |
} | |
/* | |
* Determine whether any anagram of the string can be a palindrome or not | |
* | |
* length of string >=1 | |
* if the string has only one char - odd number | |
* string length %2 == 0, all should be even number | |
* string length %2 == 1, only one char's count is odd number | |
* | |
* lower case English letter - 26 or 256 | |
*/ | |
public static bool canBePalindrome(string s) | |
{ | |
if (s == null || s.Length == 0) | |
return false; | |
int MaxSize = 256; | |
int[] countA = new int[MaxSize]; | |
for(int i =0; i< s.Length; i++) | |
{ | |
countA[s[i]]++; | |
} | |
int oddCount = 0; | |
for(int i = 0; i< MaxSize; i++) | |
{ | |
if(isLowerCaseEnglishLetter(i) && countA[i] %2 ==1) // bug: not s[i], should be i | |
{ | |
oddCount++; | |
if (oddCount > 1) | |
return false; | |
} | |
} | |
return true; | |
} | |
private static bool isLowerCaseEnglishLetter(int value) | |
{ | |
int n = value-'a'; | |
if (n >= 0 && n < 26) | |
return true; | |
else | |
return false; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment