Given a string s, return the longest palindromic substring in s.
Example 1:
Input: s = "babad" Output: "bab" Explanation: "aba" is also a valid answer.
Example 2:
Input: s = "cbbd" Output: "bb"
Constraints:
1 <= s.length <= 1000s consist of only 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 approach to finding the longest palindromic substring involves checking every single possible substring within the given text. For each substring, we then determine if it reads the same forwards and backward.
Here's how the algorithm would work step-by-step:
def longest_palindromic_substring_brute_force(input_string): longest_palindrome_found = "" # We need to check every possible substring, so we iterate through all start and end points. for starting_index in range(len(input_string)): for ending_index in range(starting_index, len(input_string)): current_substring = input_string[starting_index : ending_index + 1] # A palindrome reads the same forwards and backwards. if current_substring == current_substring[::-1]: # We update our longest palindrome if the current one is longer. if len(current_substring) > len(longest_palindrome_found): longest_palindrome_found = current_substring return longest_palindrome_foundThe most efficient way to find the longest palindrome is to consider every possible center of a palindrome and expand outwards. This avoids checking every single substring and instead intelligently checks for palindromes around potential middle points.
Here's how the algorithm would work step-by-step:
def longest_palindromic_substring(input_string):
if not input_string:
return ""
start_index_of_longest_palindrome = 0
end_index_of_longest_palindrome = 0
def expand_around_center(left_pointer, right_pointer):
nonlocal start_index_of_longest_palindrome
nonlocal end_index_of_longest_palindrome
# Expand outwards as long as characters match and pointers are within bounds.
while left_pointer >= 0 and right_pointer < len(input_string) and input_string[left_pointer] == input_string[right_pointer]:
current_length = right_pointer - left_pointer + 1
longest_length_so_far = end_index_of_longest_palindrome - start_index_of_longest_palindrome + 1
# Update the longest palindrome found if the current one is longer.
if current_length > longest_length_so_far:
start_index_of_longest_palindrome = left_pointer
end_index_of_longest_palindrome = right_pointer
left_pointer -= 1
right_pointer += 1
# Iterate through each character as a potential center for an odd-length palindrome.
for i in range(len(input_string)):
expand_around_center(i, i)
# Iterate through spaces between characters as potential centers for even-length palindromes.
for i in range(len(input_string) - 1):
expand_around_center(i, i + 1)
# Return the longest palindromic substring found.
return input_string[start_index_of_longest_palindrome : end_index_of_longest_palindrome + 1]| Case | How to Handle |
|---|---|
| Empty input string | An empty string should return an empty string as the longest palindromic substring. |
| Input string with a single character | A single character string is a palindrome by definition, so it should be returned as is. |
| Input string with all identical characters | The entire string itself is the longest palindromic substring in this scenario. |
| Input string with no palindromic substrings longer than one character | The algorithm should correctly identify and return any single character as the longest palindromic substring. |
| Input string is itself a palindrome | The algorithm should correctly return the entire input string as the longest palindromic substring. |
| Very long input strings | The chosen algorithm (e.g., Manacher's algorithm or expanding around center) must be efficient enough (O(n^2) or O(n)) to handle large inputs without timing out. |
| Multiple longest palindromic substrings of the same length | The problem statement implies returning any one of them is acceptable; the algorithm's behavior in tie-breaking should be consistent. |
| Strings containing special characters or Unicode characters | The algorithm should treat all characters equally and correctly identify palindromes regardless of character type. |