yokostan / Leetcode-Solutions

Doing exercise on Leetcode. Carry on!
0 stars 3 forks source link

Leetcode #266. Palindrome Permutation #227

Open yokostan opened 5 years ago

yokostan commented 5 years ago

HashMap:

class Solution {
    public boolean canPermutePalindrome(String s) {
        if (s == null || s.length() == 0) return false;
        HashMap<Character, Integer> map = new HashMap<Character, Integer>();

        for (int i = 0; i < s.length(); i++) {
            char c = s.charAt(i);
            if (!map.containsKey(c)) {
               map.put(c, 1);
            }
            else {
                map.remove(c);
            }
        }

        return map.size() <= 1;  
    }
}