Given string str consisting of lowercase alphabets, the task is to construct the lexicographically smallest non-palindromic string by swapping any pair of adjacent characters from the string any number of times.
If the given string cannot be converted to a lexicographically smallest non-palindromic string, then print “-1”.
Input: str = “djfbw”
Perform the following swap operations to get the lexicographically smallest non-palindromic string:
Swap ‘b’ and ‘f’, str becomes “djbfw”
Swap ‘j’ and ‘b’, str becomes “dbjfw”
Swap ‘b’ and ‘d’, str becomes “bdjfw”
Swap ‘j’ and ‘f’, str becomes “bdfjw”.
Now “bdfjw” is the lexicographically smallest string which is not a palindrome.
Input: str = “pppppp”
Naive Approach: The idea is to generate all possible permutations of the string and to check if they form palindrome or not. Print the smallest permutation among them.
Time Complexity: O(N*N!)
Auxiliary Space: O(N)
Efficient Approach: The idea is to check if lexicographically the smallest string possible from the given string is a palindrome or not. Below are the steps:
- To obtain lexicographically the smallest string, sort the given string to arrange the characters of the string in increasing order.
- Now, if the sorted string is a palindrome, then it means that the string has only one type of character and it can not be arranged to form a string which is not a palindrome.
- If it is not a palindrome, then the sorted string is lexicographically the smallest string which is not a palindrome.
Below is the implementation of the above approach:
Time Complexity: O(N)
Auxiliary Space: O(1)
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.
- Make the string lexicographically smallest and non palindromic by replacing exactly one character
- Print the last character of lexicographically smallest non-palindromic permutation of a string
- Count of Palindromic Strings possible by swapping of a pair of Characters
- Make palindromic string non-palindromic by rearranging its letters
- Lexicographically smaller string by swapping at most one character pair
- Check if a string can be converted to another by swapping of adjacent characters of given type
- Make lexicographically smallest palindrome by substituting missing characters
- Generate lexicographically smallest string of 0, 1 and 2 with adjacent swaps allowed
- Lexicographically smallest string formed by appending a character from the first K characters of a given string
- Lexicographically smallest string formed by appending a character from first K characters of a string | Set 2
- Smallest number possible by swapping adjacent even odd pairs
- Swap all occurrences of two characters to get lexicographically smallest string
- Replace minimal number of characters to make all characters pair wise distinct
- Minimum cuts required to convert a palindromic string to a different palindromic string
- Minimum non-adjacent pair flips required to remove all 0s from a Binary String
- Minimize replacement of characters to its nearest alphabet to make a string palindromic
- Length of longest Palindromic Subsequence of even length with no two adjacent characters same
- Lexicographically first palindromic string
- Find the lexicographically largest palindromic Subsequence of a String
- Lexicographically all Shortest Palindromic Substrings from a given 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.