Last active
October 21, 2022 20:56
-
-
Save gogamid/eb4cf3803bd7602a14a7cf8b11833da7 to your computer and use it in GitHub Desktop.
is palindrome permutation with bit vector
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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