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 part of a text is to systematically check every single possible continuous segment of the text to see if it reads the same forwards and backwards. By doing this exhaustive search, we are guaranteed to find the longest one because no option is left untested.
Here's how the algorithm would work step-by-step:
def longest_palindrome_brute_force(text_string):
longest_palindrome_found = ""
# Generate every possible continuous chunk to ensure no potential palindrome is missed
for start_index in range(len(text_string)):
for end_index in range(start_index + 1, len(text_string) + 1):
current_chunk = text_string[start_index:end_index]
# Verify if the chunk reads the same forwards and backwards to qualify as valid
if current_chunk == current_chunk[::-1]:
# Compare current palindrome length against the maximum found so far to track the best
if len(current_chunk) > len(longest_palindrome_found):
longest_palindrome_found = current_chunk
return longest_palindrome_foundInstead of checking every single possible piece of text from the beginning, the optimal approach treats every letter and every space between letters as the exact middle of a potential mirror image. It then checks outward from that middle as far as possible to see how large of a symmetrical piece can be formed.
Here's how the algorithm would work step-by-step:
class Solution:
def longestPalindrome(self, input_string: str) -> str:
if not input_string:
return ""
max_start_index = 0
max_length_found = 0
def expand_around_center(left_pointer: int, right_pointer: int) -> int:
while left_pointer >= 0 and right_pointer < len(input_string) and input_string[left_pointer] == input_string[right_pointer]:
left_pointer -= 1
right_pointer += 1
return right_pointer - left_pointer - 1
# We check every position as a potential single center or dual center to find all palindromes.
for center_index in range(len(input_string)):
odd_palindrome_length = expand_around_center(center_index, center_index)
even_palindrome_length = expand_around_center(center_index, center_index + 1)
current_max_length = max(odd_palindrome_length, even_palindrome_length)
# We update our global bounds only when a strictly larger palindrome is discovered.
if current_max_length > max_length_found:
max_length_found = current_max_length
max_start_index = center_index - (current_max_length - 1) // 2
# Slicing from the recorded start index to the maximum length yields the longest palindrome substring.
return input_string[max_start_index:max_start_index + max_length_found]| Case | How to Handle |
|---|---|
| Empty string or string with a length of zero | Return an empty string immediately since no valid palindromic substring can exist. |
| String with a single character | Return the character itself as any single-character string is trivially a palindrome of length one. |
| String with all identical characters | Expand around center or dynamic programming approach correctly identifies the entire string as the longest palindrome. |
| String with no palindromes longer than one character | Return any single character from the string as the default longest palindrome of length one. |
| String with multiple palindromes of the exact same maximum length | Maintain tracking variables to capture and return the first or any valid maximum-length palindromic substring encountered. |
| Maximum-sized inputs near the upper constraint limit | Use the expand-around-center approach with O(n^2) time complexity and O(1) space complexity to prevent memory limit and timeout issues. |
| Inputs with mixed case sensitivity or special unicode characters | Treat characters strictly by their exact ASCII or Unicode values unless a case-insensitive preprocessing step is explicitly required. |
| Even-length palindromes versus odd-length palindromes | Check both single-character centers and between-character centers during expansion to correctly catch both types of palindromes. |