All problems
1169MediumStringGreedy

Spoiling a Mirror Word With One Change

Tracked in this browser only
Write code

Trains the technique from

LeetCode 1328Break a Palindrome

This is an original problem, written from a brief that listed the technique, the difficulty, the topics, the function shape and the input bounds — none of that problem's wording, examples, hints or editorials. The link is there so you can map your practice onto the standard set.

Same function shape, different story and different numbers.

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 is preloaded with this. It matches the parent problem's shape, so a solution that works here transfers to a judge unchanged.

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