Break a Palindrome
Real-world version: Spoiling a Mirror Word With One ChangeThe 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.
def break_palindrome(mirror: str) -> str:public String breakPalindrome(String mirror)Checking your plan…
How this problem was checked
- Passed: Reference solution run against 15 test cases: 15 passed
- Passed: 3 worked examples confirmed by running the code
- Passed: 0 eight-word sequences shared with the parent problem's text
- Passed: Agrees with an independent brute-force solution on random inputs
- Passed: Test set shown to catch deliberately broken solutions
- Passed: Reviewed by a separate quality pass
- Passed: Function arity matches the parent problem's
More on the same topics
- Valid Palindrome IIReal-world version: Mosaic Border InspectionEasy
- Longest PalindromeReal-world version: Longest Lintel Row of Carved TilesEasy
- Minimum Number of Pushes to Type Word IReal-world version: Eight-Key Field TerminalEasy
- Largest Odd Number in StringReal-world version: Largest Odd Segment of a SerialEasy