Given a string str containing lowercase alphabets (a – z). The task is to print the string after rearranging some characters such that the string becomes non-palindromic. If its impossible to make the string non-palindrome then print -1.
Input: str = “abba”
Input: str = “zzz”
Approach: If all the characters in the string are same then no matter how you rearrange the characters, string will remain the same and will be palindromic. Now, if a non-palindromic arrangement exists, the best way to rearrange the characters is to sort the string which will form continuous segment of same characters and will never be palindromic. In order to reduce the time required to sort the string, we can store the frequencies of all the 26 characters and print them in sorted manner.
Below is the implementation of the above approach:
- Smallest Greater (than S) String of length K whose letters are subset of S
- Find the count of palindromic sub-string of a string in its sorted form
- Check if a string contains a palindromic sub-string of even length
- Find a palindromic string B such that given String A is a subsequense of B
- Lexicographically first palindromic string
- Find all palindromic sub-strings of a given string | Set 2
- Count palindromic characteristics of a String
- Count All Palindromic Subsequence in a given String
- Print all palindromic partitions of a string
- Given a string, print all possible palindromic partitions
- Find all distinct palindromic sub-strings of a given string
- Number of strings of length N with no palindromic sub string
- Minimum changes required to make first string substring of second string
- Rearrange the string to maximize the number of palindromic substrings
- Find the lexicographically largest palindromic Subsequence of a 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.