Game of Thrones - I
Problem Statement :
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 aabbccdd. 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. Constraints 1 <= |s| <= 10^5 s contains only lowercase letters in the range ascii[a . . . z ]
Solution :
Solution in C :
In C++ :
#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
#include <string>
using namespace std;
int main() {
int a[26]={0};
string s;
cin>>s;
int l=s.length();
for(int i=0;i<l;i++)
{
(a[s[i]-'a'])++;
}
int c=0;
for(int i=0;i<26;i++)
{
if(a[i]%2)
c++;
}
if(c>1)
cout<<"NO";
else
cout<<"YES";
return 0;
}
In Java :
import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;
public class Solution {
static String s;
static int hash[];
public static void main(String[] args) {
hash=new int[26];
Scanner sc = new Scanner(System.in);
s=sc.next();
s.toUpperCase();
int i;
for(i=0;i<s.length();i++)
{
int temp;
temp=s.charAt(i)-'a';
hash[temp]=hash[temp]+1;
}
int odd=0;
int set=0;
for(i=0;i<26;i++)
{
if(hash[i]%2!=0)
{
if(odd==1){set=1;break;}
else odd=1;
}
}
if(set==0)
System.out.println("YES");
else if(set==1)
System.out.println("NO");
}
}
In C :
#include<stdio.h>
int main()
{
int a[26],i,tp;
char c;
for (i=0;i<26;i++)
a[i]=0;
while ((c=getchar())!=EOF&&c!='\n')
{
a[c-97]++;
}
tp=1;
for (i=0;i<26;i++)
{
if (a[i]%2!=0&&tp==1)
tp=0;
else if (a[i]%2!=0&&tp==0)
{
printf("NO");
break;
}
}
if (i==26)
printf("YES");
return 0;
}
In Python3 :
string = input()
character_count = {}
for i in string:
if i not in character_count:
character_count[i] = string.count(i)
odd_counts = 0
for i in character_count:
if character_count[i] % 2 == 1:
odd_counts += 1
if odd_counts > 1:
print("NO")
else:
print("YES")
View More Similar Problems
Find Merge Point of Two Lists
This challenge is part of a tutorial track by MyCodeSchool Given pointers to the head nodes of 2 linked lists that merge together at some point, find the node where the two lists merge. The merge point is where both lists point to the same node, i.e. they reference the same memory location. It is guaranteed that the two head nodes will be different, and neither will be NULL. If the lists share
View Solution →Inserting a Node Into a Sorted Doubly Linked List
Given a reference to the head of a doubly-linked list and an integer ,data , create a new DoublyLinkedListNode object having data value data and insert it at the proper location to maintain the sort. Example head refers to the list 1 <-> 2 <-> 4 - > NULL. data = 3 Return a reference to the new list: 1 <-> 2 <-> 4 - > NULL , Function Description Complete the sortedInsert function
View Solution →Reverse a doubly linked list
This challenge is part of a tutorial track by MyCodeSchool Given the pointer to the head node of a doubly linked list, reverse the order of the nodes in place. That is, change the next and prev pointers of the nodes so that the direction of the list is reversed. Return a reference to the head node of the reversed list. Note: The head node might be NULL to indicate that the list is empty.
View Solution →Tree: Preorder Traversal
Complete the preorder function in the editor below, which has 1 parameter: a pointer to the root of a binary tree. It must print the values in the tree's preorder traversal as a single line of space-separated values. Input Format Our test code passes the root node of a binary tree to the preOrder function. Constraints 1 <= Nodes in the tree <= 500 Output Format Print the tree's
View Solution →Tree: Postorder Traversal
Complete the postorder function in the editor below. It received 1 parameter: a pointer to the root of a binary tree. It must print the values in the tree's postorder traversal as a single line of space-separated values. Input Format Our test code passes the root node of a binary tree to the postorder function. Constraints 1 <= Nodes in the tree <= 500 Output Format Print the
View Solution →Tree: Inorder Traversal
In this challenge, you are required to implement inorder traversal of a tree. Complete the inorder function in your editor below, which has 1 parameter: a pointer to the root of a binary tree. It must print the values in the tree's inorder traversal as a single line of space-separated values. Input Format Our hidden tester code passes the root node of a binary tree to your $inOrder* func
View Solution →