You are viewing a single comment's thread. Return to all 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; }

elaborate .. plz?

Figure out whether any anagram of the string can be a palindrome or not.

A palindrome of even length should have all character counts even. A palindrome of odd length should have all but one character counts even.

the given input string contains only lower case english letters, which are 26 of them. I am masking the bits for each character occurences.

let me take an example:

If our given string is even length palindrome then "mask" will always be zero going by above logic.

If our given string is odd length palindrome then "mask" will always be power of 2 (to check if given integer is power of 2 :-> (n & (n - 1) == 0)).

hope it helps.

yup , got it.. thanks for help.

It worked! Thanks.

## Game of Thrones - I

You are viewing a single comment's thread. Return to all comments →

bit shifting is fun :)

elaborate .. plz?

Figure out whether any anagram of the string can be a palindrome or not.A palindrome of even length should have all character counts even. A palindrome of odd length should have all but one character counts even.

the given input string contains only lower case english letters, which are 26 of them. I am masking the bits for each character occurences.

let me take an example:

abba:- Here we have 2 occurences of 'a' and 'b'.abcba:- Here we have 2 occurneces of 'a' and 'b' and one occurence of 'c'.If our given string is even length palindrome then "mask" will always be zero going by above logic.

If our given string is odd length palindrome then "mask" will always be power of 2 (to check if given integer is power of 2 :-> (n & (n - 1) == 0)).

hope it helps.

yup , got it.. thanks for help.

It worked! Thanks.