VOOZH about

URL: https://www.geeksforgeeks.org/dsa/check-characters-given-string-can-rearranged-form-palindrome/

⇱ Palindrome Permutation or Anagram - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Palindrome Permutation or Anagram

Last Updated : 14 Apr, 2026

Given a string s, check if the characters of the given string can be rearranged to form a palindrome.

Input: s = "geeksgeeks"
Output: True
Explanation: The string can be converted into a palindrome: kgeesseegk

Input: s = "geeksforgeeks "
Output: False
Explanation: More than one character has odd frequency, so it cannot form a palindrome.

Common Idea used in all approaches: A set of characters can form a palindrome if at most one character occurs an odd number of times and all characters occur an even number of times.

[Naive Approach] Using Two Loop - O(n^2) Time and O(1)

The idea is to count the frequency of each character by comparing it with every other character using nested loops. A string can form a palindrome if at most one character has an odd frequency.

  • For each character in the string, compare it with all other characters to count its frequency.
  • Use two loops (nested) to calculate frequency for every character.
  • While counting, track how many characters have odd frequency.
  • If more than one character has an odd count, the string cannot form a palindrome.
  • If at most one character has an odd count, the string can form a palindrome.

Output
True
False

[Better Approach-1] Using Frequency Count - O(n) Time and O(1) Space

First, traverse the string and store the frequency of each character in a count array. Then, check the count array to see how many characters have odd frequency. If more than one character has an odd count, it is not possible to form a palindrome; otherwise, it is possible.

Dry run for s = "geeksgeeks":

  1. Traverse "geeksgeeks" and count characters i.e, g:2, e:4, k:2, s:2.
  2. Check each count, all characters appear even number of times.
  3. Odd frequency count = 0 (no character has odd occurrence).
  4. Since at most one odd is allowed and here none exist.
  5. So, palindrome rearrangement is possible, return true.

Output
True
False

[Better Approach-2] Using Bit Manipulation (Bitmask) - O(n) Time and O(1) Space

The idea is to track the frequency of characters using a bitmask instead of counting each one separately. If character set is limited (say only lowercase, i.e., 26 characters), a single integer's bits (total 32 bits typically) can be used to track odd frequencies.

  • For every character in the string, toggle its corresponding bit using XOR.
  • If a character appears an even number of times, its bit becomes 0.
  • If a character appears an odd number of times, its bit becomes 1.
  • After processing all characters, check the final bitmask.
  • If the bitmask has at most one bit set (i.e., at most one character has odd frequency), the string can form a palindrome.
  • Otherwise, the string cannot form a palindrome.

Output
True
False
Comment
Article Tags: