Rearrange characters to form palindrome if possible in C++


We are given with a string ‘str’ of any given length. The task is to rearrange the characters in such a manner that the output will be a palindrome string without adding or removing a character from the given input string. Palindrome string is the one in which the characters are arranged in such a manner that they pronounce the same from start and last.

Let us see various input output scenarios for this −

Input − string str = "itnin"

Output − Rearrangement of characters to form palindrome if possible is: nitin

Explanation − We are given a string type variable let’s say, str. Now we will rearrange the characters of an input string in such a manner that it will be a palindrome string and if it’s not possible then it will return ‘NOT POSSIBLE’. So the output with the given input string is ‘nitin’.

Input − string str = "baaaba"

Output − Rearrangement of characters to form palindrome if possible is: aabbaa

Explanation − We are given a string type variable let’s say, str. Now we will rearrange the characters of an input string in such a manner that it will be a palindrome string and if it’s not possible then it will return ‘NOT POSSIBLE’. So the output with the given input string is ‘aabbaa’.

Approach used in the below program is as follows

  • Input a variable of string type, let’s say, str and calculate the size of a string and store it in a length named variable.

  • Pass the data to the function Rearrangement(str, length).

  • Inside the function Rearrangement(arr, length)

    • Create a variable as ‘um’ of type unordered_map that is storing char and integer type pairs.

    • Declare an integer type variable as total and set it with 0.

    • Create a character type variable as ‘ch’ and string type variables as str_1 and str_2.

    • Start loop FOR from i to 0 till i is less than length. Inside the loop, set um[str[i]] by an incremental value of 1.

    • Start loop FOR to iterate map ‘um’. Inside the loop, check IF it.second % 2 not equals to 0 then increment the total by 1 and set ch with it.first.

    • Check IF total greater than 1 OR total = 1 AND length % 2 = 0 then return 0.

    • Start loop FOR to iterate map ‘um’. Inside the loop, set str(it.second / 2, it.first), str_1 to str_1 + str and str_2 to str + str_2.

    • Check IF total = 1 then return str_1 + ch + str_2. ELSE, return str_1 + str_2.

  • Print the result.

Example

#include <bits/stdc++.h>
using namespace std;
string Rearrangement(string str, int length){
   unordered_map<char, int> um;
   int total = 0;
   char ch;
   string str_1 = "";
   string str_2 = "";

   for (int i = 0; i < length; i++){
      um[str[i]]++;
   }
   for(auto it : um){
      if(it.second % 2 != 0){
         total++;
         ch = it.first;
      }
   }
   if(total > 1 || total == 1 && length % 2 == 0){
      return 0;
   }
   for(auto it : um){
      string str(it.second / 2, it.first);
      str_1 = str_1 + str;
      str_2 = str + str_2;
   }
   if(total == 1){
      return str_1 + ch + str_2;
   }
   else{
      return str_1 + str_2;
   }
}
int main(){
   string str = "itnin";
   int length = str.size();
   cout<<"Rearrangement of characters to form palindrome if possible is: "<<Rearrangement(str, length);
   return 0;
}

Output

If we run the above code it will generate the following Output

Rearrangement of characters to form palindrome if possible is: nitin

Updated on: 02-Nov-2021

784 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements