- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP

- 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

- 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
- Program to find lexicographically smallest string after applying operations in Python
- Find the lexicographically largest palindromic Subsequence of a String in Python
- Program to find lexicographically smallest string to move from start to destination in Python
- Program to find lexicographically smallest subsequence of size k in Python
- Program to find lexicographically smallest lowercase string of length k and distance n in Python
- Lexicographically Smallest Equivalent String in C++
- Find the lexicographically smallest string which satisfies the given condition in Python
- Program to find out the palindromic borders in a string in python
- Program to find lexicographically largest mountain list in Python
- Queries to answer the X-th smallest sub-string lexicographically in C++
- Program to find smallest string with a given numeric value in Python
- Program to find length of longest palindromic substring in Python
- Program to find length of longest palindromic subsequence in Python

Advertisements