Game of Thrones – I | Lei Jiang Coding
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.
A palindrome permutation of the given string(aaabbbb) is bbaaabb.
http://javaprogrammingcodes.hol.es/games-of-thrones/
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.
A palindrome permutation of the given string(aaabbbb) is bbaaabb.
int
main() {
string s;
cin>>s;
int
flag = 1;
// Assign Flag a value of 0 or 1 depending on whether or not you find what you are looking for, in the given string
int
n = s.size();
int
map[ 26 ] = { 0 };
for
(
int
i = 0; i < n; ++i )
map[ s[i] -
'a'
] ++;
int
oddNum = 0;
for
(
int
i = 0; i < 26; ++i ){
if
( ( map[i] % 2 != 0 ) && ( ( ++oddNum == 1 ) && ( n % 2 == 0 ) || ( oddNum == 2 ) ) ){
flag = 0;
break
;
}
}
if
(flag==0)
cout<<
"NO"
;
else
cout<<
"YES"
;
return
0;
}
private static String checkString(Map<Character, Integer> m)
{
int count=0;
for(Map.Entry<Character, Integer> entry : m.entrySet())
{
int i = entry.getValue();
if(!(i%2==0))
{
count++;
if(count>1)
return "NO";
}
}
return "YES";
}
private static Map<Character, Integer> stringToMap(String inputString)
{
Map<Character, Integer> m = new HashMap<Character, Integer>();
if( inputString != null && inputString.length()>0 )
{
for( int i=0 ;i<inputString.length() ; i++ )
{
char c = inputString.charAt(i);
if(m.get(c)!= null)
{
m.put(c, m.get(c)+1);
}
else
{
m.put(c, 1);
}
}
}
return m;
}
Read full article from Game of Thrones – I | Lei Jiang Coding