Skip to content

Latest commit

 

History

History

break-a-palindrome

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 

< Previous                  Next >

Given a palindromic string palindrome, replace exactly one character by any lowercase English letter so that the string becomes the lexicographically smallest possible string that isn't a palindrome.

After doing so, return the final string.  If there is no way to do so, return the empty string.

 

Example 1:

Input: palindrome = "abccba"
Output: "aaccba"

Example 2:

Input: palindrome = "a"
Output: ""

 

Constraints:

  • 1 <= palindrome.length <= 1000
  • palindrome consists of only lowercase English letters.

Related Topics

[String]

Hints

Hint 1 How to detect if there is impossible to perform the replacement? Only when the length = 1.
Hint 2 Change the first non 'a' character to 'a'.
Hint 3 What if the string has only 'a'?
Hint 4 Change the last character to 'b'.