Show problems as
Classic problem #1328MediumStringGreedy

Break a Palindrome

Real-world version: Spoiling a Mirror Word With One Change
Tracked in this browser only
Read Break a Palindrome on LeetCode(opens in a new tab)

The statement here is our own: the same inputs, outputs and technique.

The word mirror reads the same forwards and backwards. Change exactly one of its letters to a different lowercase letter so that the word no longer reads the same both ways.

Return the smallest word that can result, ordered as a dictionary would, or an empty string when no single change can spoil it.

Examples

Example 1

Input
mirror = "abcba"
Output
"aacba"

The first letter is already as low as it goes, so the earliest letter that can be lowered is the second, and dropping it to an a leaves the word out of step with its reverse.

Example 2

Input
mirror = "aaaa"
Output
"aaab"

Every letter is already an a, so nothing in the first half can be lowered, and raising the last letter to a b is the smallest change that spoils the mirror.

Example 3

Input
mirror = "aa"
Output
"ab"

Both letters are a, so the last one is raised to a b.

Constraints

  • 1 <= mirror.length <= 1000
  • mirror holds only lowercase English letters
  • mirror reads the same forwards and backwards

The signature

The editor starts with this. It matches the parent problem's shape, so a solution that works here works on a standard judge unchanged.

Python
def break_palindrome(mirror: str) -> str:
Java
public String breakPalindrome(String mirror)