class Solution:
def breakPalindrome(self, palindrome: str) -> str:
n = len(palindrome)
if n == 1:
return ""
chars = list(palindrome)
for i in range(n // 2):
if chars[i] != 'a':
chars[i] = 'a'
return "".join(chars)
chars[-1] = 'b'
return "".join(chars)
class Solution {
public:
string breakPalindrome(string palindrome) {
int n = palindrome.size();
if (n == 1) return "";
for (int i = 0; i < n / 2; ++i) {
if (palindrome[i] != 'a') {
palindrome[i] = 'a';
return palindrome;
}
}
palindrome[n - 1] = 'b';
return palindrome;
}
};
class Solution {
public String breakPalindrome(String palindrome) {
int n = palindrome.length();
if (n == 1) return "";
char[] chars = palindrome.toCharArray();
for (int i = 0; i < n / 2; i++) {
if (chars[i] != 'a') {
chars[i] = 'a';
return new String(chars);
}
}
chars[n - 1] = 'b';
return new String(chars);
}
}
var breakPalindrome = function(palindrome) {
if (palindrome.length === 1) return "";
let arr = palindrome.split('');
for (let i = 0; i < Math.floor(arr.length / 2); i++) {
if (arr[i] !== 'a') {
arr[i] = 'a';
return arr.join('');
}
}
arr[arr.length - 1] = 'b';
return arr.join('');
};
You are given a palindromic string palindrome
consisting of lowercase English letters. Your task is to replace exactly one character in the string so that the resulting string is not a palindrome and is the lexicographically smallest possible among all such results. If it is impossible to achieve this (for example, if the string's length is 1), return an empty string ""
.
""
.At first glance, it may seem that we need to try changing every character in the palindrome and check if the resulting string is still a palindrome. However, this brute-force approach would be inefficient, especially for longer strings.
Instead, let's think about what makes a string the lexicographically smallest. For English lowercase letters, 'a' is the smallest. So, to make the smallest string, we want the leftmost character that is not 'a' to become 'a'. However, we also need to make sure the string is no longer a palindrome after the change.
For palindromes, changing a character in the first half will always break the symmetry, except for the middle character in odd-length strings (since it mirrors itself). So, we only need to look at the first half of the string for a character to change.
If all characters in the first half are already 'a', the only way to break the palindrome is to change the last character (which is mirrored in the first half) to 'b', the next smallest letter.
""
.This approach is efficient because it only examines up to half the string and makes a single change.
Let's use the input palindrome = "abccba"
as an example.
Another example: palindrome = "aabaa"
The optimized approach is much faster and uses minimal extra space.
The key to solving "Break a Palindrome" efficiently is to recognize that the lexicographically smallest string is achieved by changing the leftmost non-'a' character to 'a', but only in the first half of the palindrome. If all such characters are already 'a', changing the last character to 'b' is the best alternative. This strategy avoids unnecessary computation and ensures the result is both valid and optimal.