Palindrome Permutation

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

For example, "code" -> False, "aab" -> True, "carerac" -> True.

Solution

public class Solution {
       public boolean canPermutePalindrome(String s) {
        if (s == null || s.length() == 0) return false;
        if (s.length() == 1) return true;

        Map<Character, Integer> map = new HashMap<>();
        for (char c : s.toCharArray()) {
            map.merge(c, 1, (a, b) -> a + b);
        }

        int oddCount = 0;
        for(Character key: map.keySet()) {
            if (map.get(key) % 2 == 1) {
                oddCount +=1;
            }
        }

        if (oddCount > 1) {
            return false;
        } else {
            return true;
        }
    }
}

results matching ""

    No results matching ""