*LeetCode-Palindrome Permutation

这个题本身没啥 只是实现的时候 我一般就会用array 26来count 当然这个题不是只有小写字母 所以用了hashmap

看答案的话用set更方便 就是每次假如已经有了就remove 没有就add 这样保证一对一对的都已经remove了 最后set剩下一个或者没有剩下的话就是对的

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


你可能感兴趣的:(*LeetCode-Palindrome Permutation)