Java Solution using Bit Manipulation


  • 0
    R
    class Solution {
        public boolean canPermutePalindrome(String s) {
            if (s == null || s.length() < 1) return true;
            //int vector = 0;
            BitSet vector = new BitSet(256);
            for (int i = 0; i < s.length(); i++) {
                //vector ^= 1 << (s.charAt(i) - 'a');
                vector.flip(s.charAt(i));
            }
            //return vector == 0 || (vector & (vector - 1)) == 0;
            return vector.cardinality() == 0 || vector.cardinality() == 1;
        }
    }
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.