Hackerrank - Game of Thrones - I Solution
Dothraki are planning an attack to usurp King Robert's throne. King Robert learns of this conspiracy from Raven and plans to lock the single door through which the enemy can enter his kingdom.
For example, given the string , one way it can be arranged into a palindrome is .
Complete the gameOfThrones function below to determine whether a given string can be rearranged into a palindrome. If it is possible, return
YES, otherwise return
gameOfThrones has the following parameter(s):
- s: a string to analyze
A single line which contains , the input string.
- contains only lowercase letters in the range
A single line which contains
Sample Input 0
Sample Output 0
A palindromic permutation of the given string is bbaaabb.
Sample Input 1
Sample Output 1
Palindromes longer than 1 character are made up of pairs of characters. There are none here.
Sample Input 2
Sample Output 2
An example palindrome from the string: ddcceefeeccdd.
Solution in Python
from collections import Counter def gameOfThrones(s): add = 0 for x in Counter(s).values(): add+=x%2 return "NO" if add>1 else "YES" print(gameOfThrones(input()))
Using list comprehension
from collections import Counter def gameOfThrones(s): return "NO" if sum(x%2 for x in Counter(s).values())>1 else "YES" print(gameOfThrones(input()))