Longest Palindromic Substring

To solve this coding challenge, we need to find the longest palindromic substring within a given string
s
. A palindromic substring reads the same forward and backward. Several methods can be used to approach this problem, but a common and efficient one involves expanding around the center of potential palindromes.

Explanation

Overview
The problem requires identifying the longest substring which reads the same from both ends. For instance, in the string "babad", both "bab" and "aba" are valid answers, and in "cbbd", "bb" is a valid answer. To solve this efficiently, we will use the "expand-around-center" technique.
Detailed Steps
  1. Initialization : Check the edge case where the input string
    s
    is empty, returning an empty string immediately if true. Initialize variables to track the maximum length of palindromic substring found (
    max_len
    ) and its starting index (
    start
    ).
  2. Iterate Over the String : Loop through each character in the string, treating each character and each gap between characters as potential centers to expand around.
  3. Expand Around Centers : For each character position in the string, we will expand around two possible centers:
    • A single character as the center (
      expandAroundCenter(s, i, i)
      )
    • A pair of consecutive characters as the center (
      expandAroundCenter(s, i, i + 1)
      )
  4. Update Maximum Length : For each of these expansions, calculate the length of the palindromic substring. If this length is greater than the previously recorded maximum length, update the maximum length and the starting index of this substring.
  5. Return Result : After processing all characters, return the substring from the starting index
    start
    with length
    max_len
    .
  6. Detailed Steps in Pseudocode
    Below is the detailed pseudocode complete with comments:
                                                
    # Function to find longest palindromic substring
    function longestPalindrome(s):
    # Edge case: If string is empty, return an empty string
    if length of s is 0:
    return ""
    
    # Initialize variables to track maximum length and starting index of the longest palindromic substring
    max_length = 0
    start_index = 0
    
    # Iterate through each character in the string
    for each index i in the range from 0 to length of s - 1:
    # Expand around the center at a single character
    length1 = expandAroundCenter(s, i, i)
    
    # Expand around the center at a gap between two characters
    length2 = expandAroundCenter(s, i, i + 1)
    
    # Determine the maximum length from the two possible expansions
    max_length_here = max(length1, length2)
    
    # If the current maximum length is greater than the previously recorded maximum length
    if max_length_here > max_length:
    max_length = max_length_here
    # Update the starting index for this substring
    start_index = i - (max_length_here - 1) // 2
    
    # Return the longest palindromic substring found
    return substring of s from start_index to start_index + max_length
    
    # Helper function to expand around the center and find the length of the palindromic substring
    function expandAroundCenter(s, left, right):
    # Copy left and right pointers
    L = left
    R = right
    
    # Expand outwards as long as the characters at L and R are equal and within bounds
    while L >= 0 and R < length of s and s[L] is equal to s[R]:
    # Move left pointer to the left
    L -= 1
    # Move right pointer to the right
    R += 1
    
    # Return the length of the palindromic substring found
    return R - L - 1
    
                                            
    Step-by-Step Explanation
  7. Initialization :
    • If the input string
      s
      is empty, return an empty string to handle the edge case.
    • Set
      max_length
      to 0 to track the length of the longest palindromic substring found.
    • Set
      start_index
      to 0 to track the starting index of this substring.
  8. Iteration :
    • Loop through each index
      i
      of the string
      s
      .
    • For every position, check palindromes centered at
      s[i]
      (
      expandAroundCenter(s, i, i)
      ) and between
      s[i]
      and
      s[i+1]
      (
      expandAroundCenter(s, i, i + 1)
      ).
  9. Expand Around Center :
    • expandAroundCenter(s, left, right)
      checks equality of characters at positions
      L
      and
      R
      , expanding outward until they differ or bounds are exceeded.
    • The length of this palindrome is
      R - L - 1
      .
  10. Update Maximum Length :
    • Use
      max_length_here
      to compare current palindrome lengths (
      length1
      and
      length2
      ).
    • Update
      max_length
      and
      start_index
      if current palindrome is the longest found.
  11. Return Result :
    • Extract the substring from
      start_index
      to
      start_index + max_length
      and return it.
This approach ensures we efficiently find the longest palindromic substring by considering all possible centers and expanding around them without unnecessary computations.