Break a Palindrome

Given a palindromic string `palindrome` of lowercase English letters, replace exactly one character with any lowercase letter so that the result is not a palindrome and is the lexicographically smallest string possible. Return the result, or an empty string if no replacement can break the palindrome.

30:00

Break a Palindrome
medium
Free written solution
Topics
Companies

Given a palindromic string palindrome of lowercase English letters, replace exactly one character with any lowercase letter so that the result is not a palindrome and is the lexicographically smallest string possible. Return the result, or an empty string if no replacement can break the palindrome.

Example 1:
Input: {"palindrome":"abccba"}
Output: aaccba
Constraints:
  • 1 <= palindrome.length <= 1000

  • palindrome consists of only lowercase English letters.

Test Cases
Input (Stdin)
Expected Output (Stdout)
Execution Result

Run code to see results.

Input
arr ={"palindrome":"abccba"}

The string has 6 characters. Each character on the left half is tied to its mirror on the right; changing one side of a pair breaks the palindrome.

mirror pairs

a0b1c2c3b4a5

scanner (left half)

already 'a'

broken pair

middle (own mirror)

left half checked

0 / 3

rule

first non-'a' on the left becomes 'a'; if none, last char becomes 'b'

Variables
VariableValue
n6
DepthFunction Call
Stack empty
0/5