Suppose we have a string s that is a palindrome. We have to change one character such that s is no longer a palindrome and it is lexicographically smallest.
So, if the input is like s = "level", then the output will be "aevel", as we can change the first "l" to "a" to get the lexicographically smallest string that is not palindrome.
To solve this, we will follow these steps −
Let us see the following implementation to get better understanding −
class Solution: def solve(self, s): for i in range(len(s) // 2): if s[i] != "a": s = list(s) s[i] = "a" return "".join(s) s = list(s) s[-1] = "b" return "".join(s) ob = Solution() s = "level" print(ob.solve(s))