- Practice
- Algorithms
- Strings
- Game of Thrones - I
- Discussions

# Game of Thrones - I

# Game of Thrones - I

+ 32 comments If you are stuck you can follow this logic:

If len(str) is even, count of each elemnt should be even.

If len(str) is odd, count of ONLY one element should be odd, counts of all other elements should be even.

+ 4 comments But the Dothraki never cross the seas. How did they invade Westeros?

I kid.. I kid

+ 1 comment Here's a more in-depth explanation of the counting solution.

The problem asks you to divide a string into two mirrored parts. For example:

[abababab]

[aabb][bbaa]

A letter in the string can appear an odd or an even number of times. Let's consider what happens when the number of letters that appears an odd number of times varies.

0 "odd letters":

[aabbaabb]

[----][----]

Each letter on the left side must have a pair on the right side. If the number is even, it means we can definitely divide this string into two mirrored parts, as each letter does have a pair.

1 "odd letters":

[aabbaabbccc]

[----]ccc[----]

c[----]c[----]c

When we have 1 "odd" letter, the string can only be a palindrome if the letter without a pair is in the middle, between the two mirrored parts. Therefore, we can make a palindrome in this case.

2 or more "odd letters":

[aabbaabbcccddd]

[----]ccc[----]

c[----]c[----]c

In the case of one "odd" letter, we can put the unpaired one in the middle. But we only have one middle, so if the number of "odd" letters is any higher than 1, there is no possible position we can put the second "odd" letter and still have the string be a palindrome.

+ 2 comments bit shifting is fun :)

int main() { string s; cin>>s; int flag = 0; uint64_t mask = 0x0; for (int i = 0; i < s.length(); i++) mask = mask ^ (1 << (s[i]-'a')); if ((!mask) || (((mask & (mask - 1)) == 0))) flag = 1; if(flag==0) cout<<"NO"; else cout<<"YES"; return 0; }

+ 0 comments Best Solution in Java to pass all the testcases

// Complete the gameOfThrones function below. static String gameOfThrones(String s) { String a="abcdefghijklmnopqrstuvwxyz"; int oc=0,ec=0; int l=s.length(); for(int i=0;i<26;i++) { int c=0; for(int j=0;j<l;j++) { if(a.charAt(i)==s.charAt(j)) c++; } if(c%2==0) ec++; else oc++; } if(oc<=1) return "YES"; else if(oc==2&&l%2==0) return "YES"; else return "NO"; }

Sort 824 Discussions, By:

Please Login in order to post a comment