Fundamentals/Break a Palindrome
← PrevNext →
Given a string s of lowercase English letters, replace exactly one character with any lowercase letter so that the resulting string is NOT a palindrome and is the lexicographically smallest such string. Return that string, or an empty string "" if it is impossible (no single replacement can make it a non-palindrome).
Input
s: a non-empty string of lowercase English letters
Output
The lexicographically smallest non-palindrome obtainable by changing exactly one character, or "" if impossible
Examples
####Example 1:
Input: s = "hello"
Output: "aello"
Explanation: Changing the first character to a makes the smallest possible string, and "aello" is not a palindrome.
####Example 2:
Input: s = "abcde"
Output: "aacde"
Explanation: The first character is already a; changing the next character to a gives the smallest non-palindrome, "aacde".
####Example 3:
Input: s = "a"
Output: ""
Explanation: A single character is always a palindrome, and any replacement is still a single character, so it is impossible.