1328. Break a Palindrome LeetCode Solution
In this guide, you will get 1328. Break a Palindrome LeetCode Solution with the best time and space complexity. The solution to Break a Palindrome problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.
Table of Contents
- Problem Statement
- Complexity Analysis
- Break a Palindrome solution in C++
- Break a Palindrome solution in Java
- Break a Palindrome solution in Python
- Additional Resources
Problem Statement of Break a Palindrome
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.
Complexity Analysis
- Time Complexity: O(n)
- Space Complexity: O(n)
1328. Break a Palindrome LeetCode Solution in C++
class Solution {
public:
string breakPalindrome(string palindrome) {
if (palindrome.length() == 1)
return "";
for (int i = 0; i < palindrome.length() / 2; ++i)
if (palindrome[i] != 'a') {
palindrome[i] = 'a';
return palindrome;
}
palindrome.back() = 'b';
return palindrome;
}
};
/* code provided by PROGIEZ */
1328. Break a Palindrome LeetCode Solution in Java
class Solution {
public String breakPalindrome(String palindrome) {
if (palindrome.length() == 1)
return "";
StringBuilder sb = new StringBuilder(palindrome);
for (int i = 0; i < palindrome.length() / 2; ++i)
if (palindrome.charAt(i) != 'a') {
sb.setCharAt(i, 'a');
return sb.toString();
}
sb.setCharAt(sb.length() - 1, 'b');
return sb.toString();
}
}
// code provided by PROGIEZ
1328. Break a Palindrome LeetCode Solution in Python
class Solution:
def breakPalindrome(self, palindrome: str) -> str:
if len(palindrome) == 1:
return ''
ans = list(palindrome)
for i in range(len(palindrome) // 2):
if palindrome[i] != 'a':
ans[i] = 'a'
return ''.join(ans)
ans[-1] = 'b'
return ''.join(ans)
# code by PROGIEZ
Additional Resources
- Explore all LeetCode problem solutions at Progiez here
- Explore all problems on LeetCode website here
Happy Coding! Keep following PROGIEZ for more updates and solutions.