Java Solution runs in 1ms


  • 0
    C
        public boolean canPermutePalindrome(String s) {
            boolean[] idxArr = new boolean[256];
            Arrays.fill(idxArr, true);
            
            int oddCount = 0;
            
            for(int i=0; i<s.length(); i++){
                int idx = (int) s.charAt(i);
                idxArr[idx] = !idxArr[idx];
                
                if(idxArr[idx]){
                    oddCount --;
                }
                else{
                    oddCount ++;
                }
            }
            
            // if s is odd length the odd count should be 1 else it should be 0 
            return (s.length() % 2 == 0) ? oddCount == 0 : oddCount == 1;
        }
    

  • 0
    S

    If you use the hashtable as the counter, you can save more store area which reduce the space complexity.


Log in to reply
 

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