Find longest palindrome formed by removing or shuffling chars from string in C++

C++Server Side ProgrammingProgramming

Concept

With respect of a given string, determine the longest palindrome that can be formed by removing or shuffling characters from the string. Finally return only one palindrome if it hasbeen observed that there is multiple palindrome strings of longest length.

Input

pqr

Output

p OR q OR r

Input

ppqqrr

Output

pqrrqp OR qprrpq OR rqppqr OR
any other palindromic string of length 6.

Input

pqp

Output

pqp

Method

Here, We can partition any palindromic string into three parts – beg, mid and end. With respectof palindromic string of odd length say 2n + 1, here ‘beg’ consists of first n characters of the string, ‘mid’ consists of only 1 character that means (n + 1)th character and ‘end’ consists of last n characters of the palindromic string. With respect of palindromic string of even length 2n, there will be always empty in ‘mid’. We already knowthat ‘end’ will be reverse of ‘beg’ with respect of order for string to be palindrome.Now the concept is to implement above observation in our solution. Because shuffling of characters is permitted, there is no matter of order of characters in the input string. Now we first obtain frequency of each character in the input string. After that all characters having even occurrence (say 2n)in the input string will be part of the output string because we can easily set n characters in ‘beg’ string and the other n characters in the ‘end’ string (with the help of preserving the palindromic order). With respect of characters having odd occurrence (say 2n + 1), here, we fill ‘mid’ with one of all such characters and remaining 2n characters are partitioned in halves and added at starting and end.

Example

 Live Demo

// C++ program to find the longest palindrome by removing
// or shuffling characters from the given string
#include <bits/stdc++.h>
using namespace std;
// Shows function to find the longest palindrome by removing
// or shuffling characters from the given string
string findLongestPalindrome(string str1){
   // Indicated to stores freq of characters in a string
   int count1[256] = { 0 };
   // Determine freq of characters in the input string
   for (int i = 0; i < str1.size(); i++)
      count1[str1[i]]++;
   // Shows any palindromic string consisting of three parts
   // beg1 + mid1 + end1
   string beg1 = "", mid1 = "", end1 = "";
   //Here solution assumes only lowercase characters are
   // present in string. We can easily extend this
   // to consider any set of characters
   for (char ch1 = 'a'; ch1 <= 'z'; ch1++){
      // Now if the current character freq is odd
   if (count1[ch1] & 1){
      // Here mid1 will contain only 1 character. It
      // will be overridden with next character
      // with odd freq
      mid1 = ch1;
      // Here decrement the character freq to make
      // it even and consider current character
      // again
      count1[ch1--]--;
   }
   // Here if the current character freq is even
   else{
      // Now if count is n(an even number), push
      // n/2 characters to beg string and rest
      // n/2 characters will form part of end
      // string
      for (int i = 0; i < count1[ch1]/2 ; i++)
         beg1.push_back(ch1);
      }
   }
   // Here end will be reverse of beg
   end1 = beg1;
   reverse(end1.begin(), end1.end());
   // Now return palindrome string
   return beg1 + mid1 + end1;
}
// Driver code
int main(){
   string str1 = "pqqprrs";
   cout << findLongestPalindrome(str1);
   return 0;
}

Output

pqrsrqp
raja
Published on 24-Jul-2020 15:26:17
Advertisements