Given a string s
, return the longest palindromic substring in s
.
For example:
s = "babad"
, the output should be "bab"
or "aba"
because both are valid palindromic substrings and have the longest length.s = "cbbd"
, the output should be "bb"
.Clarifications:
s
is empty?s
consists only of digits and 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:
The brute force method for finding the longest palindromic section within a piece of text is like checking every single possible section. We consider every starting point and every possible ending point to identify sections. Then, we simply determine if each section we discover is actually a palindrome.
Here's how the algorithm would work step-by-step:
def longest_palindrome_brute_force(input_string):
longest_palindrome = ""
for starting_index in range(len(input_string)):
for ending_index in range(starting_index, len(input_string)):
substring = input_string[starting_index:ending_index + 1]
# Check if the substring is a palindrome
if substring == substring[::-1]:
#Keep track of the longest palindrome found
if len(substring) > len(longest_palindrome):
longest_palindrome = substring
return longest_palindrome
The best way to find the longest palindrome involves strategically expanding around each possible center. We efficiently check for palindromes by growing outwards, skipping many unnecessary comparisons. This avoids the need to examine every single possible substring.
Here's how the algorithm would work step-by-step:
def longest_palindromic_substring(input_string):
string_length = len(input_string)
longest_palindrome = ""
for center_index in range(string_length):
# Check for odd length palindromes.
left_index = center_index
right_index = center_index
# Expand around the center.
while left_index >= 0 and right_index < string_length and input_string[left_index] == input_string[right_index]:
current_palindrome = input_string[left_index:right_index + 1]
# Update longest palindrome if needed
if len(current_palindrome) > len(longest_palindrome):
longest_palindrome = current_palindrome
left_index -= 1
right_index += 1
for center_index in range(string_length - 1):
# Check for even length palindromes.
left_index = center_index
right_index = center_index + 1
# Expand around the center
while left_index >= 0 and right_index < string_length and input_string[left_index] == input_string[right_index]:
# Extract the current palindrome.
current_palindrome = input_string[left_index:right_index + 1]
# Update longest palindrome if needed.
if len(current_palindrome) > len(longest_palindrome):
longest_palindrome = current_palindrome
left_index -= 1
right_index += 1
return longest_palindrome
Case | How to Handle |
---|---|
Null or empty input string | Return an empty string or handle the null case appropriately (e.g., throw an exception depending on requirements). |
Single character input string | Return the single character string as it is trivially a palindrome. |
Two character input string that is a palindrome | Return the entire two-character string. |
Two character input string that is not a palindrome | Return either of the single characters, as they are both palindromes of length 1. |
Input string with all identical characters | Return the entire string as it's a palindrome. |
Maximum length string (1000 characters) | Ensure the chosen algorithm's time complexity is efficient enough to handle this size within reasonable time constraints (e.g., O(n^2) or better). |
String with multiple palindromic substrings of the same maximum length | The algorithm should return only one of these substrings - the first one found is usually acceptable if the prompt does not explicitly ask for 'all' palindromes. |
String with very long non-palindromic sequences and a short palindromic sequence | The algorithm should efficiently identify the palindromic sequence without excessive computation on the non-palindromic portions. |