No need to use HashMap at all


  • 0

    Beside the solution below, using Set is also a good solution, or BitSet, but the idea is the same.

    public class Solution {
        public boolean canPermutePalindrome(String s) {
            int[] map = new int[128];
            for (char c : s.toCharArray()) {
                map[c]++;
            }
            int countOne = 0;
            for (int i = 0; i < 128; i++) {
                if (map[i] % 2 != 0) countOne++; 
            }
            if (countOne > 1) return false;
            return true;
        }
    }
    

Log in to reply
 

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