Leetcode - Palindrome Permutation

My code:

public class Solution {
    public boolean canPermutePalindrome(String s) {
        if (s == null || s.length() == 0) {
            return true;
        }
        
        HashMap<Character, Integer> map = new HashMap<Character, Integer>();
        for (int i = 0; i < s.length(); i++) {
            char curr = s.charAt(i);
            if (!map.containsKey(curr)) {
                map.put(curr, 1);
            }
            else {
                map.put(curr, map.get(curr) + 1);
            }
        }
        
        int oddCounter = 0;
        for (Character c : map.keySet()) {
            int curr = map.get(c);
            if (curr % 2 == 1) {
                oddCounter++;
            }
            if (oddCounter >= 2) {
                return false;
            }
        }
        return true;
    }
}

简单题,想法很直接。

Anyway, Good luck, Richardo! — 09/19/2016

    原文作者:Richardo92
    原文地址: https://www.jianshu.com/p/15fdafd6162f#comments
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞