Given a string, Check if characters of the given string can be rearranged to form a palindrome.
For example characters of “geeksogeeks” can be rearranged to form a palindrome “geeksoskeeg”, but characters of “geeksforgeeks” cannot be rearranged to form a palindrome.
A set of characters can form a palindrome if at most one character occurs odd number of times and all characters occur even number of times.
A simple solution is to run two loops, the outer loop picks all characters one by one, the inner loop counts number of occurrences of the picked character. We keep track of odd counts. Time complexity of this solution is O(n2).
We can do it in O(n) time using a count array. Following are detailed steps.
1) Create a count array of alphabet size which is typically 256. Initialize all values of count array as 0.
2) Traverse the given string and increment count of every character.
3) Traverse the count array and if the count array has more than one odd values, return false. Otherwise return true.
Below is the implementation of above approach.
This article is contributed by Abhishek. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above
- Check if a string can be rearranged to form special palindrome
- Check if the characters in a string form a Palindrome in O(1) extra space
- Check if characters of one string can be swapped to form other
- Rearrange characters to form palindrome if possible
- Minimal moves to form a string by adding characters or appending string itself
- Check if a doubly linked list of characters is palindrome or not
- Find the player who rearranges the characters to get a palindrome string first
- Minimum number of characters to be replaced to make a given string Palindrome
- Minimum characters to be added at front to make string palindrome
- Check if a given string is a rotation of a palindrome
- Check if any anagram of a string is palindrome or not
- Check if it is possible to create a palindrome string from given N
- C Program to Check if a Given String is Palindrome
- Check if a string is palindrome in C using pointers
- Check whether second string can be formed from characters of first string
Improved By : Ita_c