Given palindromic string s, the task is to find minimum k, such that you can cut this string into k+1 parts, and then unite them in such a way that the final string will be a palindrome and it won’t be equal to the initial string s. If it is impossible then print -1.
Input : string = "civic" Output : 2 Explanation : ci | v | ic --> ic | v | ci --> icvci Input : string = "gggg" Output : -1 Input : string = "redder" Output : 1 Explanation : red | der --> der | red --> derred Input : string = "aaaasaaaa" Output : -1
Approach 1: It is given that formed palindromic string should be different from the given string.
So when our string consists of n or n-1 (when n is odd) equal characters, then there is no way to get the answer. For example –
String : "aaaasaaaa" String : "aaaa"
Above strings can not form palindrome other than the given one.
Otherwise, cut the longest prefix of s of length l, that consists of equal characters of length equal to l-1. Now similarly cut suffix of length l-1, and call remaining part as mid.
Now we have prefix = s[1..l] and suff = s[(n-l+1)..n]. Swap prefix and suffix, then unite all three parts together and keep mid as it is.
prefix + mid + suffix suffix + mid + prefix
So clearly we can get the answer in two cuts. Finally you just have to check if it is possible to get answer in one cut. For that just cut one element from end and append it at front and continue this cyclic shift. During this if we get a palindromic string other then the given one then it means we can get answer in just one cut.
Below is the implementation of above approach:
Time Complexity : O(N2)
Efficient Approach: Again if our string consists of n or n-1 (when n is odd) equal characters, then there is no way to get the answer.
Now, divide this problem into two parts that whether the string length is even or odd.
If the string length is odd then we always have a middle element in it so just make 2 cuts around the middle element and split the string into three segments and swap first and third segments.
Say, we have a string:
nolon --> no | l | on --> on | l | no --> onlno
If the string length is even then check whether the half string is itself a palindromic string or not.
If so then:
- Split a string recursively into two parts and check whether the resulting half string is a palindrome or not.
- If string became of odd length then simply return 2.
asaasa --> as | aa | sa --> sa | aa | as --> saaaas
- If resulting string is not a palindrome then return 1.
toottoot --> to | ottoot --> ottoot | to --> ottootto
Else we can cut this string from the middle, form two segments and swap each other.
voov --> vo | ov --> ov | vo --> ovvo
Below is the implementation of above approach:
Time Complexity : O(N)
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 equal palindromic cuts with rearrangements allowed
- Minimum Count of Bit flips required to make a Binary String Palindromic
- Minimum number of given operations required to convert a string to another string
- Minimum swaps required to convert one binary string to another
- Minimum operations required to convert a binary string to all 0s or all 1s
- Minimum given operations required to convert a given binary string to all 1's
- Minimum number of subsequences required to convert one string to another
- Minimum number of subsequences required to convert one string to another using Greedy Algorithm
- Minimum flips required to convert given string into concatenation of equal substrings of length K
- Minimum operations required to convert all characters of a String to a given Character
- Make palindromic string non-palindromic by rearranging its letters
- Minimum steps to convert all paths in matrix from top left to bottom right as palindromic paths | Set 2
- Minimum steps to convert all paths in matrix from top left to bottom right as palindromic paths
- Minimum splits required to convert a number into prime segments
- Longest Palindromic Substring using Palindromic Tree | Set 3
- Minimum changes required to make first string substring of second string
- Minimum number of palindromic subsequences to be removed to empty a binary string
- Minimum size substring to be removed to make a given string palindromic
- Convert string X to an anagram of string Y with minimum replacements
- Minimum rotations required to get the same 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 firstname.lastname@example.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.