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 <= 1000
s
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 method finds the longest palindrome by checking every possible piece of the input. It's like trying every possible answer until you find the right one. We will go through each possible substring and check if it's a palindrome.
Here's how the algorithm would work step-by-step:
def longest_palindromic_substring_brute_force(input_string):
longest_palindrome = ""
input_length = len(input_string)
# Iterate through all possible starting positions
for starting_index in range(input_length):
# Iterate through all possible ending positions
for ending_index in range(starting_index, input_length):
substring = input_string[starting_index:ending_index + 1]
# Check if the substring is a palindrome
if substring == substring[::-1]:
# Update longest palindrome if current substring is longer
if len(substring) > len(longest_palindrome):
longest_palindrome = substring
return longest_palindrome
The best way to find the longest palindrome inside a string is to think about it growing outward from the middle. We check every possible center of a palindrome and expand to see how big it can get.
Here's how the algorithm would work step-by-step:
def longest_palindromic_substring(input_string):
longest_palindrome = ""
string_length = len(input_string)
for i in range(string_length):
# Check for odd length palindromes.
left_index = i
right_index = i
# Expand around center, i.
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 the longest palindrome if needed.
if len(current_palindrome) > len(longest_palindrome):
longest_palindrome = current_palindrome
left_index -= 1
right_index += 1
# Check for even length palindromes.
left_index = i
right_index = i + 1
# This loop expands around a potential center, i & i+1.
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 the 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 string input | Return an empty string or handle as an error, depending on requirements specification. |
String with a single character | Return the single character string itself since it's a palindrome. |
String with two identical characters (e.g., 'aa') | Return the entire string as it's a palindrome. |
String with two different characters (e.g., 'ab') | Return either 'a' or 'b', as they are the longest palindromic substrings of length 1. |
String with all identical characters (e.g., 'aaaa') | Return the entire string since it's a palindrome. |
Very long string to test for performance | Ensure the algorithm has at least O(n^2) or better time complexity to avoid timeouts. |
String with many overlapping palindromes | Algorithm needs to correctly identify and select the longest among possibly many overlapping palindromes. |
String with no palindromic substring longer than 1 character | Return any single character substring, since the problem guarantees at least one exists. |