Skip to content
  • Home
  • Contact Us
  • About Us
  • Privacy Policy
  • DMCA
  • Linkedin
  • Pinterest
  • Facebook
thecscience

TheCScience

TheCScience is a blog that publishes daily tutorials and guides on engineering subjects and everything that related to computer science and technology

  • Home
  • Human values
  • Microprocessor
  • Digital communication
  • Linux
  • outsystems guide
  • Toggle search form
HackerRank Game of Thrones - I Problem Solution

HackerRank Game of Thrones – I Problem Solution

Posted on April 28, 2023May 6, 2023 By Yashwant Parihar No Comments on HackerRank Game of Thrones – I Problem Solution

In this post, we will solve HackerRank Game of Thrones – I Problem 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.

But, to lock the door he needs a key that is an anagram of a palindrome. He starts to go through his box of strings, checking to see if they can be rearranged into a palindrome. Given a string, determine if it can be rearranged into a palindrome. Return the string YES or NO.
Example
s= ’aabbccdd’
One way this can be arranged into a palindrome is abcddcba. Return YES.

Function Description
Complete the gameOfThrones function below.

gameOfThrones has the following parameter(s):

  • string s: a string to analyze

Returns

  • string: either YES or NO

Input Format

A single line which contains s.

Sample Input 0

aaabbbb

Sample Output 0

YES

Explanation 0

A palindromic permutation of the given string is bbaaabb.

Sample Input 1

cdefghmnopqrstuvw

Sample Output 1

NO

Explanation 1

Palindromes longer than 1 character are made up of pairs of characters. There are none here.

Sample Input 2

cdcdcdcdeeeef

Sample Output 2

YES

Explanation 2

An example palindrome from the string: ddcceefeeccdd.

HackerRank Game of Thrones - I Problem Solution
HackerRank Game of Thrones – I Problem Solution

Table of Contents

  • Game of Thrones – I C Solution
  • Game of Thrones – I C++ Solution
  • Game of Thrones – I C Sharp Solution
  • Game of Thrones – I Java Solution
  • Game of Thrones – I JavaScript Solution
  • Game of Thrones – I Python Solution

Game of Thrones – I C Solution

char s[100001];
int a[128];

int main()
{
	int i,j;
	while(~scanf("%s",s))
	{
		memset(a,0,sizeof a);
		for(i=0;s[i];i++) a[s[i]]++;
		for(i=j=0;i<128;i++) j+=a[i]&1?1:0;
		puts(j<2?"YES":"NO");
	}
	return 0;
}

Game of Thrones – I C++ Solution

#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
void Solve()
{
	string s;
	cin >> s;
	vector<int> cc('z'-'a', 0);
	for(auto &c: s)
	{
		++cc[c-'a'];
	}
	int max_odd=s.size()%2?1:0;
	for(auto &x: cc)
	{
		if(x%2)
		{
			if(--max_odd<0)
			{
				printf("NO");
				return;
			}
		}
	}
	printf("YES");
}

int main()
{
	int t=1;
//	cin >> t;
	for(int tc=1; tc<=t; ++tc)
	{
		Solve();
	}
}

Game of Thrones – I C Sharp Solution

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace GameOfThrones_I
{
    public class Solution
    {
        static void Main(string[] args)
        {
            string word = Console.ReadLine();
            Console.WriteLine(hasPalindrome(word));
        }

        public static string hasPalindrome(string word)
        {
            int[] charCount = new int[26];
            foreach (char c in word)
            {
                charCount[(int)c - (int)'a']++;
            }
            int oddCount = 0;
            foreach (int count in charCount)
            {
                if (count % 2 != 0) oddCount++; 
            }
            if (oddCount > 1)
            {
                return "NO";
            }
            return "YES";
        }
    }
}

Game of Thrones – I Java Solution

import java.io.*;
import java.math.*;
import java.security.*;
import java.text.*;
import java.util.*;
import java.util.concurrent.*;
import java.util.function.*;
import java.util.regex.*;
import java.util.stream.*;
import static java.util.stream.Collectors.joining;
import static java.util.stream.Collectors.toList;

class Result {

    /*
     * Complete the 'gameOfThrones' function below.
     *
     * The function is expected to return a STRING.
     * The function accepts STRING s as parameter.
     */

    public static String gameOfThrones(String s) {
    // Write your code here
        char[] chars = s.toCharArray();
        int length = chars.length;
        int[] counts = new int[26];
        
        for(int i = 0; i <length; i++){
            int index = chars[i] - 'a';
            counts[index]++;
        }
        boolean once = true;
        for(int i = 0; i < counts.length; i++){
            // One odd is allowed if input length is odd
            if(once && length%2 != 0 && counts[i]%2 != 0){
                    once = false;
                    continue;
            }
            if(counts[i]%2 != 0){
                return "NO";
            }
        }
        return "YES";
    }

}

public class Solution {
    public static void main(String[] args) throws IOException {
        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
        BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(System.getenv("OUTPUT_PATH")));

        String s = bufferedReader.readLine();

        String result = Result.gameOfThrones(s);

        bufferedWriter.write(result);
        bufferedWriter.newLine();

        bufferedReader.close();
        bufferedWriter.close();
    }
}

Game of Thrones – I JavaScript Solution

var input = '';

function solve(s) {
    var chars = {},
        i,
        c,
        odd,
        letter;
  
  s.trim();

    for (i = 0; i < s.length - 1; i++) {
      c = s.charAt(i);
      if (chars[c]) {
        chars[c] += 1;
      } else {
        chars[c] = 1;
      }
    }

    odd = false;
    for (letter in chars) {
      if (chars[letter] % 2 !== 0) {
        if (odd === true) {
          return "NO";
        }
        odd = true;
      }
    }

    return "YES";
}

function run () {
  process.stdout.write(solve(input));
}

process.stdin.resume();
process.stdin.setEncoding('ascii');
process.stdin.on('data', function (_input) { input += _input; });
process.stdin.on('end', function () { run(); });

Game of Thrones – I Python Solution

string = input().strip()

char_count = dict()

for char in string:
    if char in char_count:
        char_count[char] += 1
    else:
        char_count[char] = 1
        
num_odds = 0
for char in char_count:
    if char_count[char] % 2 == 0: continue
    else: num_odds += 1

if num_odds > 1: print("NO")
else: print("YES")
    

Other Solutions

  • HackerRank Two Strings Problem Solution
  • HackerRank String Construction Problem Solution
c, C#, C++, HackerRank Solutions, java, javascript, python Tags:C, cpp, CSharp, Hackerrank Solutions, java, javascript, python

Post navigation

Previous Post: HackerRank Making Anagrams Problem Solution
Next Post: HackerRank Two Strings Problem Solution

Related Posts

HackerRank Two Robots Problem Solution HackerRank Two Robots Problem Solution c
HackerRank Matrix Problem Solution HackerRank Matrix Problem Solution c
HackerRank Roads in HackerLand Problem Solution HackerRank Roads in HackerLand Solution c
HackerRank Counting Special Sub-Cubes Problem Solution HackerRank Counting Special Sub-Cubes c
HackerRank Extra Long Factorials Problem Solution HackerRank Extra Long Factorials Problem Solution c
HackerRank Huarongdao Problem Solution HackerRank Huarongdao Problem Solution c

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Pick Your Subject
Human Values

Copyright © 2023 TheCScience.

Powered by PressBook Grid Blogs theme