Given a string, write a function to determine whether it is one of the permutations of a palindrome string.

A palindrome is a word or phrase that is the same in both directions. Permutation is the rearrangement of letters.

Palindromes are not necessarily dictionary words.

Example 1:

Input: “tactcoa” Output: true (permutations: “tacocat”, “atcocta”, etc.)

java

class Solution { public boolean canPermutePalindrome(String s) { char[] arr = s.toCharArray(); HashMap<Character, Integer> hm = new HashMap<>(); // Iterates through the character array to get each character and stores the character in a two-column collectionfor(char c: arr) {// If the set does not contain the key, the character is stored as a key, and if the set contains the key, the value is stored as 1if(! Hm.containskey (c)) {// If this key is not included hm.put(c, 1); }else{ hm.put(c, hm.get(c) + 1); } } int cot = 0; // Prints a two-column collection to get the number of occurrences of charactersfor(Character Key: hm.keyset ()) {// hm.keyset () represents the set of all keysif(hm.get(key)%2!=0){
                cot = cot +1;
            }
        }
        if(cot>1){
            return false;
        }
        else{
            return true; }}}Copy the code

python

class Solution(object):
    def canPermutePalindrome(self, s):
        dicta = {}
        for i in s:
            dicta[i]=s.count(i)
        cot = 0
        for value in dicta.values():
            if value%2! =0:
                cot=cot+1
        if cot>1:
            return False
        else:
            return True
Copy the code

The basic idea is to check the number of occurrences of each character in the string and return true if all of them are even, true if one of them is not even, and false if more than one character occurs.