Java straight forward solution with HashMap


  • 0
    public boolean canPermutePalindrome(String s) {
            HashMap<Character, Integer> map = new HashMap<Character, Integer>();
            for(int i = 0; i < s.length(); i ++){
                if(!map.containsKey(s.charAt(i))){
                    map.put(s.charAt(i), 1);
                }else{
                    map.put(s.charAt(i), map.get(s.charAt(i)) + 1);
                }
            }
            int oddCount = 0;
            for(Integer count : map.values()){
                if(count % 2 == 0) continue;
                if((s.length() % 2 == 0 && ++oddCount > 0) || (s.length() % 2 == 1 && ++oddCount > 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.