Longest Palindromic Substring Problem

Longest Palindromic Substring Problem

Problem Description: Given a string s, find the longest palindromic substring in s. A palindrome is a string that reads the same forward and backward.

Solution Process:

  1. Problem Understanding
    A palindromic substring must satisfy: when expanding from the center outward, the characters on both sides are completely symmetrical. For example, "aba" is a palindrome, and "abba" is also a palindrome. We need to find the longest such contiguous substring in the given string.

  2. Brute Force Solution (Basic Idea)
    The simplest approach is to check all possible substrings:

  • Enumerate all starting positions i and ending positions j (where i ≤ j)
  • Check if the substring s[i...j] is a palindrome
  • Record the longest palindromic substring found

Time Complexity: O(n³), where checking a palindrome takes O(n), and there are O(n²) substrings in total.

  1. Center Expansion Method (Optimized Solution)
    Observing the symmetrical nature of palindromes, each palindrome has a center:
  • Odd-length palindrome: the center is a single character (e.g., the center of "aba" is 'b')
  • Even-length palindrome: the center is between two identical characters (e.g., the center of "abba" is between the two 'b's)

Algorithm Steps:

  1. Iterate through each position in the string as a potential center
  2. For each center, expand outward until the palindrome condition is no longer satisfied
  3. Record the longest palindromic substring found

Implementation Details:

def longestPalindrome(s):
    def expandAroundCenter(left, right):
        # Expand from the center outward
        while left >= 0 and right < len(s) and s[left] == s[right]:
            left -= 1
            right += 1
        # Return the start and end indices of the palindrome
        return left + 1, right - 1
    
    start, end = 0, 0
    for i in range(len(s)):
        # Odd-length palindrome, center is a single character
        left1, right1 = expandAroundCenter(i, i)
        # Even-length palindrome, center is between two characters
        left2, right2 = expandAroundCenter(i, i + 1)
        
        # Update the longest palindrome
        if right1 - left1 > end - start:
            start, end = left1, right1
        if right2 - left2 > end - start:
            start, end = left2, right2
    
    return s[start:end+1]

Time Complexity: O(n²), Space Complexity: O(1)

  1. Dynamic Programming Method (Alternative Approach)
    Define dp[i][j] to indicate whether the substring s[i...j] is a palindrome:
  • Base case: A single character is always a palindrome (dp[i][i] = true)
  • Two identical characters form a palindrome (dp[i][i+1] = true if s[i] == s[i+1])
  • State transition: dp[i][j] = true if and only if s[i] == s[j] and dp[i+1][j-1] = true

This method also has O(n²) time complexity but requires O(n²) space.

  1. Manacher's Algorithm
    A more advanced algorithm with O(n) time complexity, but its implementation is more complex. The core idea is to leverage known palindrome information to avoid redundant calculations.

Conclusion: The center expansion method is the most practical solution for interviews, balancing efficiency and implementation difficulty.