- Related Questions & Answers
- Program to find kth smallest n length lexicographically smallest string in python
- Program to find Lexicographically Smallest String With One Swap in Python
- Find the lexicographically largest palindromic Subsequence of a String in Python
- Program to find lexicographically smallest subsequence of size k in Python
- Lexicographically Smallest Equivalent String in C++
- Find the lexicographically smallest string which satisfies the given condition in Python
- Program to find length of longest palindromic substring in Python
- Program to find length of longest palindromic subsequence in Python
- Queries to answer the X-th smallest sub-string lexicographically in C++
- Program to Find Out the Smallest Substring Containing a Specific String in Python
- Python program to find Largest, Smallest, Second Largest, and Second Smallest in a List?
- Python program to find the smallest number in a list
- Find all distinct palindromic sub-strings of a given String in Python
- Program to find Smallest and Largest Word in a String in C++
- Program to check given string is anagram of palindromic or not in Python

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

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 −

- for i in range 0 to integer part of(size of s / 2), do
- if s[i] is not same as "a", then
- s := a new list from all characters in s
- s[i] := "a"
- join all characters in s and return

- if s[i] is not same as "a", then
- s := a new list from all characters in s
- last element of s := "b"
- join all characters in s and return

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))

"level"

aevel

Advertisements