Readable JAVA solution


  • 0
    C

    put them in the Hash, and check if there are more than 2 odd count characters.
    the most odd character in palindrome is one!! which can be put in the middle

    public class Solution {
        public boolean canPermutePalindrome(String s) {
            char[] ca = s.toCharArray();
            HashMap<Character, Integer> map = new HashMap<Character, Integer>();
            for (int i=0; i<ca.length; i++) {
                if(map.containsKey(ca[i])) {
                    map.put(ca[i], map.get(ca[i]) + 1);
                } else {
                    map.put(ca[i], 1);
                }
            }
            
            boolean foundodd = false;
            for (char c : map.keySet()) {
                if (map.get(c) % 2 == 1) {
                    if (foundodd) {
                        return false;
                    } else {
                        foundodd = true;
                    }
                }
            }
            
            return true;
        }
    }
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.