Scale customer reach and grow sales with AskHandle chatbot

How to Find the Longest Palindromic Substring in a String?

In many programming interviews, you might be asked to find the longest palindromic substring within a larger string. A palindrome is a sequence of characters that reads the same forwards and backwards, such as "racecar" or "abba". Identifying the longest substring that is a palindrome can seem challenging at first, but there are multiple strategies to solve this problem efficiently. Here's a clear explanation along with a perfect code example in Python.

image-1
Written by
Published onJune 18, 2025
RSS Feed for BlogRSS Blog

How to Find the Longest Palindromic Substring in a String?

In many programming interviews, you might be asked to find the longest palindromic substring within a larger string. A palindrome is a sequence of characters that reads the same forwards and backwards, such as "racecar" or "abba". Identifying the longest substring that is a palindrome can seem challenging at first, but there are multiple strategies to solve this problem efficiently. Here's a clear explanation along with a perfect code example in Python.

Understanding the Problem

Given a string, you need to find the longest contiguous substring that reads the same backward and forward. For example, if the input is "babad", the longest palindromic substrings could be "bab" or "aba", both having a length of 3. In such cases, the output should be one of these substrings, depending on your implementation.

Common Approaches

1. Expand Around Center

This approach is quite intuitive. For each character in the string (and each gap between characters), you treat it as a potential center of a palindrome and expand outwards while the characters on both sides are identical.

Here's how it works:

  • Consider each character as the center of an odd-length palindrome.
  • Consider the gap between every pair of consecutive characters as a center for an even-length palindrome.
  • Expand from the center as long as the characters on both sides are the same.
  • Keep track of the maximum length palindrome found during this process.

This approach runs in O(n^2) time and uses constant extra space.

2. Dynamic Programming

This method involves building a table that keeps track of whether a substring is a palindrome. Starting with substrings of length 1, then 2, and so on, you can determine whether longer substrings are palindromes based on smaller substrings.

While dynamic programming helps in understanding and later can be optimized, it generally uses O(n^2) space and time.

3. Manacher's Algorithm

This is a more advanced and efficient algorithm that finds the longest palindrome in linear time, but it's more complex to implement. For most interview scenarios, the expand around center approach is usually sufficient.

Implementation: Expand Around Center

Let's see a simple, clear implementation of the expand around center method in Python:

Python

How the Code Works

  • The expandFromCenter() function takes two indices (left and right) and expands outward while the characters at those positions are equal.
  • For every index i in the string:
    • It checks for the longest odd-length palindrome centered on i.
    • It checks for the longest even-length palindrome centered between i and i+1.
  • It updates the start and end indices whenever a longer palindrome is found.
  • It returns the substring between start and end.

Example Run

Python

This function efficiently finds the longest palindromic substring with a time complexity of O(n^2) and a space complexity of O(1). It is suitable for most interview questions and practical scenarios.

Create your AI Agent

Automate customer interactions in just minutes with your own AI Agent.

Featured posts

Subscribe to our newsletter

Achieve more with AI

Enhance your customer experience with an AI Agent today. Easy to set up, it seamlessly integrates into your everyday processes, delivering immediate results.

Latest posts

AskHandle Blog

Ideas, tips, guides, interviews, industry best practices, and news.

View all posts