266. Palindrome Permutation

Given a string, determine if a permutation of the string could form a palindrome.

For example,
“code” -> False, “aab” -> True, “carerac” -> True.

Hint:

Consider the palindromes of odd vs even length. What difference do you notice?
Count the frequency of each character.
If each character occurs even number of times, then it must be a palindrome. How about character which occurs odd number of times?

// 利用set,如果set没有就存入,有的话就取出。这样最后剩下0个或者1个的时候就是回文数

public class Solution {
    public boolean canPermutePalindrome(String s) {
        if(s.length() < 2 || s == null){
            return true;
        }
        HashSet set = new HashSet();
        for(int i = 0; i < s.length(); i++){
            char cur = s.charAt(i);
            if(!set.contains(cur)){
                set.add(cur);
            } else {
                set.remove(cur);
            }
        
        }
        return set.size() == 0 || set.size() == 1;
    }
}