Can a String Be Rearranged into a Palindrome? Frequency Counting Approach
Fast recall
Quick revision
Core answer
Whether you reduce 'can rearrange to palindrome' to the at-most-one-odd-count rule instead of generating permutations
Trying to generate permutations (exponential), or forgetting that exactly one odd is allowed for odd-length strings
State the rule crisply (<=1 odd count) and track odd-count parity with a toggle for an elegant O(n) check
Interview-ready guide
Interview answer
The key realization is that I don't need to actually build any palindrome; I just need to check a property of the character counts.
A palindrome mirrors around its center, so every character has to appear an even number of times, with one exception: if the string's length is odd, exactly one character can have an odd count and it sits in the middle. So the rule is simple: a string can be rearranged into a palindrome if and only if at most one character has an odd frequency.
The brute force idea of generating all permutations and checking each one is exponential and completely unnecessary. Instead, I count the frequency of each character in a single pass, then count how many characters have an odd frequency. If that number is zero or one, the answer is yes; otherwise no.
This is O of n time and O of k space. There's an elegant implementation trick: I keep a set, and as I scan each character I toggle it, adding it if it's absent and removing it if it's present. Adding then removing cancels out for even counts, so at the end the set contains exactly the characters with odd counts.
Then I just check that the set's size is at most one.
For example, a-a-b-b-c has c as the only odd-count character, so the set ends with just c, size one, and I can form a palindrome like a-b-c-b-a. But a-a-b-b-c-d leaves both c and d in the set, size two, so it's impossible.
Can a String Be Rearranged into a Palindrome? Frequency Counting Approach
Direct Answer
Count each character's frequency, then check the parity: a string can be rearranged into a palindrome if and only if at most one character has an odd count. Even-length strings allow zero odd counts; odd-length allows exactly one (the middle char). O(n) time, O(k) space. You never build the palindrome - just count odds.
Deep Dive & Architecture
A palindrome reads the same from both ends, so almost every character must be placed in matching pairs. An even-length palindrome requires every frequency to be even. An odd-length palindrome may have exactly one odd frequency for its center.
Worked example: carrace
The counts are c=2, a=2, r=2, and e=1. Only e is odd, so the letters can be rearranged as racecar. In contrast, abc has three odd counts and cannot form a palindrome.
Count characters in a map or fixed alphabet array, then count how many frequencies are odd. The answer is true when that number is at most one. It is not necessary to generate the actual permutation.
The scan is O(n), followed by O(k) work over the distinct characters, which is O(n) overall. Storage is O(k). A set can also track odd characters by adding on an odd occurrence and removing on an even occurrence; the final set size gives the same test.
Normalization belongs to the contract. A phrase such as Tact Coa forms taco cat only if spaces are removed and letters are lowercased. Do not apply those rules unless the question asks for them.
Code Example
boolean canFormPalindrome(String s) {
Set<Character> odd = new HashSet<>();
for (char c : s.toCharArray()) {
if (!odd.add(c)) odd.remove(c); // toggle: in on odd, out on even
}
return odd.size() <= 1; // at most one odd count
}
// s = "aabbc": a even, b even, c odd -> odd={c}, size 1 -> true (e.g. abcba)
// s = "aabbcd": c, d odd -> odd={c,d}, size 2 -> false
The toggle leaves exactly the odd-count characters in the set.
Common Mistakes & Pitfalls
- Generating permutations: exponential and wholly unnecessary.
- Requiring zero odds always: odd-length strings allow exactly one odd char.
- Building the palindrome: the question only asks if it can be formed.
- Case/space sensitivity: clarify whether to normalize before counting.
Check your understanding
Answer these without looking back at the article.
- What is the frequency counting pattern and when do you apply it?
- How do you find the top K most frequent elements in an array?
- How do you group strings that are anagrams together?
Looking for a structured roadmap?
Every module on this page is also part of the full Java backend interview track.