#1328

Break a Palindrome

specialist · 875 · lc medium +31 · verified · 51.6% accepted · 2,437 likes · top 41%

Description

Given a lowercase palindrome string palindrome, replace exactly one character with any lowercase English letter so that the resulting string is not a palindrome. Among all valid replacements, return the lexicographically smallest result. If no single replacement can break the palindrome, return an empty string.

A string a is lexicographically smaller than b of the same length if at the first differing position a has a strictly smaller character — for example, "abcc" is smaller than "abcd".

Example 1:

Input: palindrome = "abccba"
Output: "aaccba"
Explanation: There are many ways to make "abccba" not a palindrome, such as "zbccba", "aaccba", and "abacba".
Of all the ways, "aaccba" is the lexicographically smallest.

Example 2:

Input: palindrome = "a"
Output: ""
Explanation: There is no way to replace a single character to make "a" not a palindrome, so return an empty string.

Code

1
2
3