string manipulation
palindrome
permutations
algorithms
coding challenge

Check if a permutation of a string can become a palindrome

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

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:

  • 'carrace works because the counts allow racecar'
  • 'aabb works because both characters occur an even number of times'
  • 'abc does 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.

python
1from collections import Counter
2
3
4def can_form_palindrome(text):
5    counts = Counter(text)
6    odd_count = sum(1 for value in counts.values() if value % 2 == 1)
7    return odd_count <= 1
8
9
10print(can_form_palindrome("carrace"))
11print(can_form_palindrome("abc"))

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.

python
1
2def can_form_palindrome_toggle(text):
3    odd_chars = set()
4
5    for ch in text:
6        if ch in odd_chars:
7            odd_chars.remove(ch)
8        else:
9            odd_chars.add(ch)
10
11    return len(odd_chars) <= 1
12
13
14print(can_form_palindrome_toggle("ivicc"))
15print(can_form_palindrome_toggle("hello"))

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.

python
1from collections import Counter
2
3
4def can_form_palindrome_normalized(text):
5    cleaned = [ch.lower() for ch in text if ch.isalnum()]
6    counts = Counter(cleaned)
7    odd_count = sum(1 for value in counts.values() if value % 2 == 1)
8    return odd_count <= 1
9
10
11print(can_form_palindrome_normalized("Tact Coa"))

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.

Course illustration
Course illustration

All Rights Reserved.