Game of Thrones – I – HackerRank Solution

In this post, we will solve Game of Thrones – I HackerRank Solution. This problem (Game of Thrones – I) is a part of HackerRank Problem Solving series.

Solution – Game of Thrones – I – HackerRank Solution

C++

#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

int main(int argc, char const *argv[])
{
    string str;
    cin >> str;
    int A[26];
    for (int i = 0; i < 26; ++i)
    {
        A[i] = 0;
    }
    for (int i = 0; i < str.length(); ++i)
    {
        A[str[i] - 'a'] ++;
    }

    int sum = 0;
    for (int i = 0; i < 26; ++i)
    {
        sum = sum + (A[i] % 2);
    }

    if (sum >= 2)
    {
        cout << "NO" << endl;
    }
    else
    {
        cout << "YES" << endl;
    }

    return 0;
}

Python

s = input()
cnt = {}
for char in s:
    if char in cnt:
        cnt[char] += 1
    else:
        cnt[char] = 1
odd = False
for key in cnt:
    if cnt[key] % 2 == 1:
        if odd:
            print("NO")
            break
        odd = True
else:
    print("YES")

Java

import java.io.*;
import java.util.*;

public class Solution {

    public static void main(String[] args) {
        /* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */
        Scanner input = new Scanner(System.in);
        String s = input.nextLine();
        
        Map<Character, Integer> letters = new HashMap<>();
        for(char c : s.toCharArray())
        {
            if(letters.containsKey(c))
                letters.put(c, letters.get(c) + 1);
            else
                letters.put(c, 1);
        }
        
        int odd = 0;
        int even = 0;
        for(Integer frequency : letters.values())
        {
            if(frequency % 2 == 1)
            {
                odd++;
                continue;
            }
                
            if(frequency % 2 == 0)
                even++;
        }
        
        if(odd > 1) 
            System.out.println("NO");
        else
            System.out.println("YES");
        
    }
}

Note: This problem (Game of Thrones – I) is generated by HackerRank but the solution is provided by CodingBroz. This tutorial is only for Educational and Learning purpose.

Leave a Comment

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