Break a Palindrome - Complete Solution Guide
Break a Palindrome is LeetCode problem 1328, a Medium level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.
Problem Statement
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 c
Detailed Explanation
The problem requires you to modify a given palindromic string by replacing exactly one character with another lowercase English letter such that the resulting string is no longer a palindrome and is the lexicographically smallest possible string after the modification. If no such replacement is possible (e.g., the input is a single character), return an empty string.
Solution Approach
The solution iterates through the first half of the palindrome string. If it encounters a character that is not 'a', it replaces that character with 'a' and returns the modified string. If all characters in the first half are 'a', it means the palindrome consists of 'a's (or mostly 'a's with a single middle character). In this case, the last character of the string is replaced with 'b' to break the palindrome. If the string length is 1, there's no way to break the palindrome, so an empty string is returned.
Step-by-Step Algorithm
- Step 1: Check if the length of the input `palindrome` is less than or equal to 1. If it is, return an empty string because a single-character palindrome cannot be broken.
- Step 2: Iterate through the first half of the `palindrome` string (up to `n // 2`).
- Step 3: Inside the loop, check if the character at the current index `i` is not equal to 'a'.
- Step 4: If the character at index `i` is not 'a', replace it with 'a'. Return the modified string immediately. This ensures that the lexicographically smallest non-palindrome is generated.
- Step 5: If the loop completes without finding a character to replace with 'a', it means the first half of the palindrome (and thus most of the string) consists only of 'a's. In this case, change the last character of the string to 'b'.
- Step 6: Return the modified string.
Key Insights
- Insight 1: To achieve the lexicographically smallest non-palindrome, try to replace the leftmost non-'a' character with 'a'. This prioritizes the beginning of the string.
- Insight 2: Only the first half of the palindrome needs to be considered because any change made in the first half will also affect the second half due to its palindromic nature.
- Insight 3: A special case exists when all characters in the first half of the palindrome are 'a'. In this scenario, changing the last character to 'b' ensures the string is no longer a palindrome.
Complexity Analysis
Time Complexity: O(n)
Space Complexity: O(n)
Topics
This problem involves: String, Greedy.
Companies
Asked at: Expedia, J.P. Morgan, MathWorks, Nvidia, VMware.