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 certain palindrome string.
The king has a string composed of lowercase English letters. Help him figure out whether any anagram of the string can be a palindrome or not.
Input Format
A single line which contains the input string.
Constraints
- length of string
- Each character of the string is a lowercase English letter.
Output Format
A single line which contains YES or NO in uppercase.
Sample Input 0
aaabbbb
Sample Output 0
YES
Explanation 0
A palindrome permutation of the given string is bbaaabb.
Sample Input 1
cdefghmnopqrstuvw
Sample Output 1
NO
Explanation 1
You can verify that the given string has no palindrome permutation.
Sample Input 2
cdcdcdcdeeeef
Sample Output 2
YES
Explanation 2
A palindrome permutation of the given string is ddcceefeeccdd.
Java Code :
import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;
public class Solution {
static String gameOfThrones(String s){
// Complete this function
int lettercount[] = new int[26];
for(char ch : s.toCharArray())
{
lettercount[ch-'a']++;
}
int counter = 0;
for(int i :lettercount)
{
if( i%2 == 1)
{
counter++;
}
if(counter >1)
break;
}
if(counter <=1)
return "YES";
else
return "NO";
}
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
String s = in.next();
String result = gameOfThrones(s);
System.out.println(result);
}
}
Can i use this page for reference to my website https://codeplay.in ?
ReplyDelete