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.
- 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 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
- Lexicographically smallest string which differs from given strings at exactly K indices
- Find a string such that every character is lexicographically greater than its immediate next character
- Lexicographically smaller string by swapping at most one character pair
- Lexicographically Smallest Permutation of length N such that for exactly K indices, a[i] > a[i] + 1
- Find lexicographically smallest string in at most one swaps
- Check if frequency of character in one string is a factor or multiple of frequency of same character in other string
- Minimum cuts required to convert a palindromic string to a different palindromic string
- Lexicographically smallest and largest anagrams of a string containing another string as its substring
- Check if a substring can be Palindromic by replacing K characters for Q queries
- Python | Replacing Nth occurrence of multiple characters in a String with the given character
- Lexicographically first palindromic string
- Find the lexicographically largest palindromic Subsequence of a String
- Lexicographically all Shortest Palindromic Substrings from a given string
- Lexicographically smallest array formed by at most one swap for every pair of adjacent indices
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.