Skip to content

Instantly share code, notes, and snippets.

@gogamid
Last active October 21, 2022 20:56
Show Gist options
  • Save gogamid/eb4cf3803bd7602a14a7cf8b11833da7 to your computer and use it in GitHub Desktop.
Save gogamid/eb4cf3803bd7602a14a7cf8b11833da7 to your computer and use it in GitHub Desktop.
is palindrome permutation with bit vector
public static boolean isPalindromePerm(String s) {
int checker = 0;
for (char c : s.toCharArray()) {
if ('a' <= c && c <= 'z') {
int val = c - 'a';
int mask = 1 << val;
if ((checker & mask) == 0) {
checker |= mask; //flip to 1
} else {
checker &= ~mask; //flip to 0
}
}
}
return checker == 0 || ((checker & (checker - 1)) == 0);//either even or odd
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment