Given a palindromic string str containing only lowercase letters, the task is to print the lexicographically smallest string, by replacing exactly one character, such the string is not a palindrome.
Input: str = “abccba”
Lexicographically smallest non-palindromic string possible is “aaccba”, here we have replaced the second letter ‘b’ by an ‘a’ which makes it a non palindrome.
Input: str = “a”
A single character is always a palindrome hence we cannot replace the value. Therefore the output is -1.
Approach: To solve the problem mentioned above we will check only half of the string and replace all characters which are not ‘a’ to the character ‘a’ itself. The edge case for the question is if there is only one character, we will return an empty string. Otherwise, if all the characters are the same then we will replace the last character only with character ‘b’.
Below is the implementation of the 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.
- Print the last character of lexicographically smallest non-palindromic permutation of a string
- Make the string lexicographically smallest and non palindromic by swapping of adjacent pair of characters
- Make palindromic string non-palindromic by rearranging its letters
- Lexicographically smallest string formed by removing at most one character
- Lexicographically smallest string whose hamming distance from given string is exactly K
- Lexicographically smallest permutation of a string that can be reduced to length K by removing K-length prefixes from palindromic substrings of length 2K
- Lexicographically smallest string which differs from given strings at exactly K indices
- 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
- Find a string such that every character is lexicographically greater than its immediate next character
- Lexicographically Smallest Permutation of length N such that for exactly K indices, a[i] > a[i] + 1
- Lexicographically smallest subsequence possible by removing a character from given string
- Lexicographically smallest string possible by inserting given character
- Lexicographically smaller string by swapping at most one character pair
- Find lexicographically smallest string in at most one swaps
- Minimum cuts required to convert a palindromic string to a different palindromic string
- Check if frequency of character in one string is a factor or multiple of frequency of same character in other string
- 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 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.