You are given a string s
. You can convert s
to a palindrome by adding characters in front of it.
Return the shortest palindrome you can find by performing this transformation.
Example 1:
Input: s = "aacecaaa" Output: "aaacecaaa"
Example 2:
Input: s = "abcd" Output: "dcbabcd"
Constraints:
0 <= s.length <= 5 * 104
s
consists of lowercase English letters only.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:
The brute force way to find the shortest palindrome involves testing every possible prefix to add to the beginning of the original string. We keep checking until we find a combination that results in a palindrome.
Here's how the algorithm would work step-by-step:
def shortest_palindrome_brute_force(original_string):
shortest_palindrome = None
for i in range(len(original_string)):
prefix_to_add = original_string[:i][::-1]
new_string = prefix_to_add + original_string
# Need to check if the new string is a palindrome.
if new_string == new_string[::-1]:
# Update shortest_palindrome if it's shorter or non-existent
if shortest_palindrome is None or len(new_string) < len(shortest_palindrome):
shortest_palindrome = new_string
# This handles the case where the original string is already a palindrome
if original_string == original_string[::-1] and (shortest_palindrome is None or len(original_string) < len(shortest_palindrome)):
shortest_palindrome = original_string
if shortest_palindrome is None:
return ""
return shortest_palindrome
The goal is to find the shortest way to make a string a palindrome by adding characters to its beginning. The best approach cleverly reuses existing information about palindromes to avoid redundant checks. We identify the longest palindrome starting from the beginning of the string, and then add the reversed remaining part to the front.
Here's how the algorithm would work step-by-step:
def shortest_palindrome(input_string):
string_length = len(input_string)
for i in range(string_length, 0, -1):
# Check for longest palindrome from start
if input_string[:i] == input_string[:i][::-1]:
longest_palindrome_prefix_length = i
break
remaining_string = input_string[longest_palindrome_prefix_length:]
reversed_remaining_string = remaining_string[::-1]
# Prepend reversed string for shortest palindrome
shortest_palindromic_string = reversed_remaining_string + input_string
return shortest_palindromic_string
Case | How to Handle |
---|---|
Null or empty string input | Return an empty string immediately as there's nothing to make a palindrome from. |
Single-character string input | Return the original string since a single character is already a palindrome. |
String that is already a palindrome | Return the original string as no modifications are needed. |
Long string with a very short non-palindromic prefix | Ensure the algorithm efficiently identifies and reverses only the minimum required prefix. |
String with a long palindromic suffix but non-palindromic prefix | The algorithm should correctly identify the longest palindromic suffix, and prepend the reverse of the prefix before it. |
String containing special characters or non-alphanumeric characters | The algorithm should treat these characters as regular characters or specify the valid range of characters. |
String with a large number of repeated characters that are not palindromic | The algorithm needs to efficiently handle potentially large intermediate strings generated during the reversing and concatenation process. |
Maximum string length exceeding memory capacity, leading to potential memory overflow | Consider using a rolling hash based approach or limiting the string length processed to prevent memory issues. |