Related Articles
Minimum equal palindromic cuts with rearrangements allowed
• Difficulty Level : Hard
• Last Updated : 21 Dec, 2017

Given a string of length n. Find the minimum number of possible cuts after rearranging the string (if required), such that each cut is a palindrome and length of every cut is equal. That is, find the minimum number of palindromes of equal lengths that can be obtained by partitioning the given string if rearrangement of string is allowed before partitioning.

Examples:

```Input : string = "aabaac"
Output : 2
Explanation : Rearrange the string as "abaaca"
and cut into "aba" and "aca"

Input : string = "aabbccdd"
Output : 1
Explanation : Rearrange the string as "abcddcba"
This is a palindrome and cannot be
cut further.
```

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

If we observe carefully, our problem reduces to calculating characters with odds and even counts. Below are the possible cases,

1. If the characters present in the string have only even counts then the answer will be 1 as we can rearrange the entire string to form a palindrome.
2. If there is only one character with odd count, then also the answer will be 1 as we can
rearrange the entire string to form a palindrome.
3. If there is more than one character with odd count, then we will create two separate list of characters – one for odd characters and one for even characters. Now, if we notice that if a character has odd count then if we subtract 1 from it, the count will become even. So we will insert the element with odd counts only once in the odd list. We will insert the elements with even counts (evenCount/2) times, i.e. half of their count in the even list. Now our problem is to uniformly distribute the even count elements among odd count elements to form palindromes of equal length. Suppose the list of even count characters is even and odd count characters is odd. If even.size() is divisible by odd.size() our answer will be odd.size() otherwise we will transfer elements from even list to odd list until even.size() is divisible by odd.size().

Below is the implementation of above idea:

 `// CPP program to find minimum number of palindromic ``// cuts of equal length ``#include``using` `namespace` `std;`` ` `// function to find minimum number of ``// palindromic cuts of equal length ``int` `minPalindromeCuts(string str)``{   ``    ``// map to store count of characters``    ``unordered_map<``char``,``int``> m;``     ` `    ``// store count of characters in a map``    ``for` `(``int` `i=0;i even;``     ` `    ``// list to store odd count characters``    ``vector<``char``> odd;``     ` `    ``for` `(``auto` `itr = m.begin(); itr!=m.end(); itr++)``    ``{  ``        ``// add odd count characters only once and ``        ``// decrement count by 1``        ``if` `(itr->second%2!=0)``        ``{``            ``odd.push_back(itr->first);``            ``itr->second--;``        ``}``    ``}``     ` `    ``for` `(``auto` `itr = m.begin(); itr!=m.end(); itr++)``    ``{``        ``if` `(itr->second%2==0)``        ``{   ``            ``// add even count characters half of their``            ``// count to the even list so that we can ``            ``// simply repeat the even list on both ``            ``// sides of an odd char to generate a ``            ``// palindrome``            ``for` `(``int` `i=0;i<(itr->second)/2;i++)            ``                ``even.push_back(itr->first);``        ``}``    ``}``     ` `    ``// if there is no odd count characters or ``    ``// only 1 odd count character, return 1``    ``if` `(odd.size() <= 1)    ``        ``return` `1;``     ` `    ``else` `    ``{``        ``// Move some characters from even list over ``        ``// to odd list to make palindrome work``        ``while` `(odd.size() > 0 && even.size() > 0 &&``               ``even.size() % odd.size() != 0) ``        ``{``            ``odd.push_back(even.back());``            ``odd.push_back(even.back());``            ``even.pop_back();``        ``}``         ` `        ``return` `odd.size();``    ``}``}`` ` `// driver code``int` `main()``{``    ``string str = ``"aabaac"``;    ``    ``cout << minPalindromeCuts(str);``    ``return` `0;``}`

Output:

```2
```

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.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

In case you wish to attend live classes with industry experts, please refer DSA Live Classes

My Personal Notes arrow_drop_up