Game of Thrones I Hackerrank problem solution

Game of Thrones I Hackerrank

We all are familiar with the Game of Thrones and love their characters, that’s why today we are going to solve a problem based on this.So the problem is like, Dothraki is planning an attack to usurp King Robert’s throne. King Robert came to know about this conspiracy from Raven and plans to lock the single door through which the enemy can enter his kingdom.

game-of-thrones-hackerrank

But, the problem comes here that to lock the door he needs a key that is an anagram of a certain palindrome string.

The king has a string composed of lowercase English letters. Now, you need to help him figure out whether any anagram of the string can be a palindrome or not.

Please help him out!!

Input Format
A single line which contains the input string.
Constraints

1 <= length of string <= 105
Each character of the string is a lowercase English letter.
Output Format
A single line which contains YES or NO in uppercase.
Sample Input: 01

aaabbbb
Sample Output: 01

YES

Explanation
A palindrome permutation of the given string is bbaaabb.
Sample Input : 02

cdefghmnopqrstuvw
Sample Output : 02

NO
Explanation
You can verify that the given string has no palindrome permutation.
Sample Input : 03

cdcdcdcdeeeef
Sample Output: 03

YES
Explanation
A palindrome permutation of the given string is ddcceefeeccdd.

Solution

Problem: Given a string. tell whether any anagram of given sub-string will be the palindrome
Approach: Suppose the length of given string is l. then, If any anagram of given string will be the palindrome if X[i] = X[l-1-i] where X is given string. So, every character has to be repeated the even number of time. Only one of the characters can happen an odd number of times which will occur in the middle of a string.

#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
 
findPalind(char *arr)
{
int l[26]={0},i;
    int odd=0; 
    while(*arr!='\0') {
        l[*arr-'a']++;
        arr++; 
    }
    for(i=0;i<26;i++) {
        if(l[i] &1)
                odd++; 
        }
    if(odd<=1)
        printf("YES"); 
    else  printf("NO"); 
    return;
}
int main() {

    char arr[100001];
    scanf("%s",arr);
    findPalind(arr);
    return 0;
}
# Enter your code here. Read input from STDIN. Print output to STDOUT
word = list(raw_input())
lst = {}
for i in word:
    if lst.get(i):
        lst[i] = (lst[i]+1)%2
    else:
        lst[i] = 1
sm = 0
for i in lst:
    sm = sm +lst[i]
if (len(word)%2 ==0 and sm ==0) or (len(word)%2 ==1 and sm ==1):
    print "YES"
else:
    print "NO"
<?php
$_fp = fopen("php://stdin", "r");
/* Enter your code here. Read input from STDIN. Print output to STDOUT */
$str= str_split(trim(fgets($_fp)));
$impares=0;
$freq = array_count_values($str);
foreach($freq as $f){
    $impares+=($f%2);
    if ($impares>1){
        break;
    }
}
if ($impares>1){
    echo "NO";
} else {
    echo "YES";
}
echo "\n";
?>
import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;

public class Solution {

    public static void main(String[] args) {
        Scanner myScan = new Scanner(System.in);
        String theStr = myScan.nextLine();
        int[] freq = new int[26];
        for(int i = 0; i < theStr.length(); i++)
        {
            freq[theStr.charAt(i) - 'a']++;
        }
        boolean singleFound = false;
        String ans = "YES";
        for(int i = 0; i < 26; i++)
        {
            if(freq[i] % 2 != 0)
            {
                if(singleFound)
                {
                    ans = "NO";
                    break;
                }
                else
                    singleFound = true;
            }
        }
        System.out.println(ans);
        myScan.close();
    }
}

 

Note: All rights reserved. No part of this Post may be copied, distributed, or transmitted in any form or by any means, without the prior written permission of the website admin, except in the case of brief quotations embodied in critical reviews and certain other noncommercial uses permitted by copyright law. For permission requests, write to the owner, addressed “Attention: Permissions Coordinator,” to the admin@coderinme.com

A web developer(Front end and Back end), and DBA at csdamu.com. Currently working as Salesforce Developer @ Tech Matrix IT Consulting Private Limited. Check me @about.me/s.saifi

Leave a reply:

Your email address will not be published.