266. 回文排列

根据回文的定义,有2种可能,(1)字符串中所有字符出现次数为偶数,(2)字符串中只有一个字符出现次数为奇数(在中间位置)

所以我们只需要统计整个字符串中是否存在1个以上出现次数为奇数的元素

时间O(n),空间O(s)(s为整个字符串的离散度)

    public boolean canPermutePalindrome(String s) {
        Map<Character,Integer> map = new HashMap<Character,Integer>();
        for (int i=0;i<s.length();i++){
           map.put(s.charAt(i), map.getOrDefault(s.charAt(i),0)+1);
        }  
        int count=0;
        for(char key:map.keySet()){
           count=count+map.get(key)%2;
        }
        return count<2;
    }
争取早日不再是一只菜鸡
原文地址:https://www.cnblogs.com/jchen104/p/14675603.html