Given a palindromic string of lowercase English letters palindrome
, replace exactly one character with any lowercase English letter so that the resulting string is not a palindrome and that it is the lexicographically smallest one possible.
Return the resulting string. If there is no way to replace a character to make it not a palindrome, return an empty string.
A string a
is lexicographically smaller than a string b
(of the same length) if in the first position where a
and b
differ, a
has a character strictly smaller than the corresponding character in b
. For example, "abcc"
is lexicographically smaller than "abcd"
because the first position they differ is at the fourth character, and 'c'
is smaller than 'd'
.
Example 1:
Input: palindrome = "abccba" Output: "aaccba" Explanation: There are many ways to make "abccba" not a palindrome, such as "zbccba", "aaccba", and "abacba". Of all the ways, "aaccba" is the lexicographically smallest.
Example 2:
Input: palindrome = "a" Output: "" Explanation: There is no way to replace a single character to make "a" not a palindrome, so return an empty string.
Constraints:
1 <= palindrome.length <= 1000
palindrome
consists of only lowercase English letters.When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
We need to change a palindrome to make it not a palindrome anymore. The brute force method involves trying every possible change to the input and checking if the result is not a palindrome. We make one modification at a time and assess if it results in a non-palindrome string.
Here's how the algorithm would work step-by-step:
def break_palindrome_brute_force(palindrome):
palindrome_length = len(palindrome)
for index in range(palindrome_length):
# Try changing each letter of the palindrome
for char_code in range(ord('a'), ord('z') + 1):
new_char = chr(char_code)
modified_palindrome = list(palindrome)
modified_palindrome[index] = new_char
modified_palindrome = "".join(modified_palindrome)
# Check if the modification resulted in a non-palindrome
if modified_palindrome != palindrome and modified_palindrome != modified_palindrome[::-1]:
return modified_palindrome
# Handle edge case where input is a single character palindrome
if palindrome_length <= 1:
return ""
# If no change resulted in a non-palindrome, it's impossible
return ""
The goal is to change a palindrome to a non-palindrome with just one change. The trick is to alter the first non-'a' character to an 'a', but we need to handle special cases like very short palindromes.
Here's how the algorithm would work step-by-step:
def break_palindrome(palindrome_string):
list_palindrome = list(palindrome_string)
palindrome_length = len(list_palindrome)
if palindrome_length <= 1:
return ""
for i in range(palindrome_length // 2):
# Find the first non-'a' character
if list_palindrome[i] != 'a':
list_palindrome[i] = 'a'
return "".join(list_palindrome)
# If all chars in the first half are 'a'
# Change the last char to 'b'
list_palindrome[palindrome_length - 1] = 'b'
return "".join(list_palindrome)
Case | How to Handle |
---|---|
Empty or null input string | Return an empty string as there is no palindrome to break. |
Single character input string (e.g., 'a') | Return an empty string because modifying a single character can never result in a non-palindrome string. |
Two character input string (e.g., 'aa') | Change the first character to 'b' resulting in 'ba'. |
Input string of all 'a's (e.g., 'aaaa') | Change the first character to 'b', resulting in 'baaa'. |
Input string with an odd length and middle character 'a' (e.g., 'aba') | Change the first 'a' to 'b', resulting in 'bba'. |
Input string with an odd length and middle character not 'a' (e.g., 'bbb') | Change the first character to 'a' resulting in 'abb'. |
Long palindrome string | The solution should iterate through the string only once, so the time complexity is O(n), making it scale efficiently. |
Palindrome that can only be broken by changing the last non-'a' character to 'a' | If the first half is all 'a's, change the last character to 'b' or, if the entire string is 'a's, change the last character to 'b'. |