Check if a permutation of a string can become a palindrome
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.
Introduction
To decide whether some permutation of a string can form a palindrome, you do not need to generate any permutations. The whole problem reduces to counting characters and checking how many of them have odd frequency.
The Key Observation
A palindrome is symmetric. That means:
- in an even-length palindrome, every character must appear an even number of times
- in an odd-length palindrome, exactly one character may appear an odd number of times
So a string can be rearranged into a palindrome if and only if at most one character has an odd count.
For example:
- '
carraceworks because the counts allowracecar' - '
aabbworks because both characters occur an even number of times' - '
abcdoes not work because three characters occur an odd number of times'
Solve It With a Frequency Counter
The straightforward implementation counts characters and then counts the odd frequencies.
This runs in linear time with respect to the string length and is the clearest version for interviews and production code alike.
A Toggle-Set Variant
You can also track odd characters directly by toggling membership in a set. Every time a character appears, add it if it is absent and remove it if it is already present.
This version avoids storing full counts and keeps only the characters with odd parity.
Decide How to Treat Spaces and Case
Many real-world variants ignore spaces, punctuation, and letter case. If that is the requirement, normalize the string first.
This returns True because the normalized letters can form tacocat.
That normalization step is often the difference between passing and failing a coding interview variant of the problem.
Why Permutations Are the Wrong Approach
A brute-force solution would generate all permutations and test whether any of them is a palindrome. That is enormously wasteful. A string of length n has n! permutations in the worst case, which becomes intractable almost immediately.
The frequency-based solution avoids all of that. It extracts the only property that matters for palindrome formation: parity of character counts.
This is a good example of replacing a combinatorial search with an invariant-based check.
Complexity
Both the frequency-counter and toggle-set solutions run in O(n) time, where n is the string length. The space cost depends on the character set in use, but for typical fixed alphabets it is effectively bounded.
That is about as efficient as the problem gets because you must inspect each character at least once.
Common Pitfalls
- Generating permutations is the wrong strategy because the problem can be solved directly from character counts.
- Forgetting that odd-length palindromes may have one odd-count character leads to rejecting valid cases.
- Ignoring normalization requirements such as case-insensitivity or space removal can produce answers that do not match the intended problem statement.
- Confusing "is already a palindrome" with "can be permuted into one" misses the actual question.
- Using a frequency array tied to one alphabet without checking the input domain can break the solution for Unicode or mixed-symbol input.
Summary
- A string can be permuted into a palindrome if at most one character has an odd frequency.
- Counting frequencies or tracking odd-parity characters both solve the problem in linear time.
- Normalization rules such as lowercasing and removing spaces matter if the problem statement requires them.
- Brute-force permutation generation is unnecessary and inefficient.
- The real insight is parity of counts, not the permutations themselves.
Related reading
- Check if a string is rotation of another WITHOUT concatenating
- check if a tree is a binary search tree
- Check if array B is a permutation of A
- Check if edge is included in SOME MST in linear time non-distinct values
- Check if box is covered by spheres
- Check if one integer is an integer power of another
- Check if given string can be created by a set of characters cut out from magazine article
- Check if one list contains element from the other

DSA Fundamentals
Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.