clueless coding // TODO: be smarter

LeetCode Palindrome Permutation: Java Solution



TL;DR Code Solution


public boolean canPermutePalindrome(String s) {

    HashMap<Character, Integer> hashMap = new HashMap<Character, Integer>();

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

    boolean hasOdd = false;
    for (int value : hashMap.values()) {
        if (value % 2 == 1) {
            if (hasOdd) {
                return false;
            }
            hasOdd = true;
        }
    }

    return true;


}