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.
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.
If we observe carefully, our problem reduces to calculating characters with odds and even counts. Below are the possible cases,
- 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.
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.
- 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:
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.
- Minimum cuts required to convert a palindromic string to a different palindromic string
- Minimum insertions to form a palindrome with permutations allowed
- Minimum minutes needed to make the time palindromic
- Minimum palindromic subarray removals to make array Empty
- Minimum number of palindromic subsequences to be removed to empty a binary string
- Make palindromic string non-palindromic by rearranging its letters
- Longest Palindromic Substring using Palindromic Tree | Set 3
- Minimum steps to convert all paths in matrix from top left to bottom right as palindromic paths
- Minimum steps to convert all paths in matrix from top left to bottom right as palindromic paths | Set 2
- Minimum length of substring whose rotation generates a palindromic substring
- Minimum move to end operations to make all strings equal
- Minimum characters to be deleted from the end to make given two strings equal
- Minimum operations to make frequency of all characters equal K
- Minimum operation to make all elements equal in array
- Minimum characters to be deleted from the beginning of two strings to make them equal
- Minimum Bitwise AND operations to make any two array elements equal
- Minimum number of given operations required to make two strings equal
- Minimum number of swaps to make two binary string equal
- Minimum length String with Sum of the alphabetical values of the characters equal to N
- Minimum swaps to make two strings equal by swapping only with third string
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. 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.