# Leetcode – Palindrome Permutation

Tags: LeetCodeGiven a string, determine if a permutation of the string could form a palindrome.

For example, “code” -> False, “aab” -> True, “carerac” -> True.

Hint:

Consider the palindromes of odd vs even length. What difference do you notice? Count the frequency of each character. If each character occurs even number of times, then it must be a palindrome. How about character which occurs odd number of times?

**Analysis**

A native solution is to generate the permutation of the string, then check whether it is a palindrome.

A better solution is suggested from the above hint.

- If each character occurs even numbers, then a permutation of the string could form a palindrome.
- If only one character occurs odd number of times, it can also form a palindrome. like
`aba, abbba.`

We can use a HashTable to count the frequency of the characters in the string. A better one is to use a HashSet, if a character is in the HashSet, and we see it again, we remove it from the HashSet. This means the character occurs even times. At the end, There are two valid possibilities:

1. The HashSet is empty, which means all the characters occur even times, returning true;

2. The HashSet contains only one character, which means only one character occurs odd times. returning true.

Otherwise, there are more than one characters occurring odd times. returning false;

## Java solution

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
import java.util.HashSet; import java.util.Set; public class PermutePalindrome { public boolean canPermutePalindrome(String s) { Set<Character> set = new HashSet<Character>(); for(int i = 0; i < s.length(); i++){ // occur even time, remove it from the set Character cur = s.charAt(i); if(set.contains(cur)){ set.remove(cur); } else { // occur odd time set.add(cur); } } // only at most one character can occur odd times return set.size() <= 1; } } |