Given a string, convert the string to palindrome without any modifications like adding a character, removing a character, replacing a character etc.
Examples:
Input : "mdaam" Output : "madam" or "amdma" Input : "abb" Output : "bab" Input : "geeksforgeeks" Output : "No Palindrome"
1. Count occurrences of all characters.
2. Count odd occurrences. If this count is greater than 1 or is equal to 1 and length of the string is even then obviously palindrome cannot be formed from the given string.
3. Initialize two empty strings firstHalf and secondHalf.
4. Traverse the map. For every character with count as count, attach count/2 characters to end of firstHalf and beginning of secondHalf.
5. Finally return the result by appending firstHalf and secondHalf
// CPP program to rearrange a string to // make palindrome. #include <bits/stdc++.h> using namespace std; string getPalindrome(string str) { // Store counts of characters unordered_map<char, int> hmap; for (int i = 0; i < str.length(); i++) hmap[str[i]]++; /* find the number of odd elements. Takes O(n) */ int oddCount = 0; char oddChar; for (auto x : hmap) { if (x.second % 2 != 0) { oddCount++; oddChar = x.first; } } /* odd_cnt = 1 only if the length of str is odd */ if (oddCount > 1 || oddCount == 1 && str.length() % 2 == 0) return "NO PALINDROME"; /* Generate first halh of palindrome */ string firstHalf = "", secondHalf = ""; for (auto x : hmap) { // Build a string of floor(count/2) // occurrences of current character string s(x.second / 2, x.first); // Attach the built string to end of // and begin of second half firstHalf = firstHalf + s; secondHalf = s + secondHalf; } // Insert odd character if there // is any return (oddCount == 1) ? (firstHalf + oddChar + secondHalf) : (firstHalf + secondHalf); } int main() { string s = "mdaam"; cout << getPalindrome(s); return 0; } |
amdma
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
Recommended Posts:
- Rearrange the characters of the string such that no two adjacent characters are consecutive English alphabets
- Check if characters of a given string can be rearranged to form a palindrome
- Check if the characters in a string form a Palindrome in O(1) extra space
- Minimum length of the sub-string whose characters can be used to form a palindrome of length K
- Longest substring whose characters can be rearranged to form a Palindrome
- Rearrange a string in the form of integer sum followed by the minimized character
- Rearrange a string so that all same characters become d distance away
- Rearrange a string so that all same characters become atleast d distance away
- Rearrange characters in a string such that no two adjacent are same
- Rearrange characters in a string such that no two adjacent are same using hashing
- Rearrange string such that no pair of adjacent characters are of the same type
- Check if it is possible to rearrange a binary string with alternate 0s and 1s
- Sentence Palindrome (Palindrome after removing spaces, dots, .. etc)
- Count all palindrome which is square of a palindrome
- Minimum insertions to form a palindrome | DP-28
- Minimum insertions to form shortest palindrome
- Minimum insertions to form a palindrome with permutations allowed
- Check if a string can be rearranged to form special palindrome
- Find index i such that prefix of S1 and suffix of S2 till i form a palindrome when concatenated
- Count pair of strings whose concatenation of substrings form a palindrome
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

