#5 - Longest Palindromic Substring - Leetcode

#5 - Longest Palindromic Substring - Leetcode

Finding the longest palindromic substring is a classic algorithm problem that often shows up in interviews. In this article, we’ll explore different approaches, their time complexities, and why the fastest known solution, Manacher’s Algorithm is still O(n) despite what appears to be nested loops.

✅ Problem Statement

Given a string s, return the longest substring which is a palindrome.

Example:

Input: "babad"
Output: "bab" // or "aba"        

🧠 What is a Palindrome?

A palindrome is a string that reads the same backward and forward, like "racecar" or "abba".

🛠️ Approach 1: Brute Force

Try all possible substrings, check each one to see if it’s a palindrome, and keep track of the longest.

Article content

  • Time Complexity: O(n³)
  • Space Complexity: O(1)
  • Status: ❌ Too slow for large strings

⚡️ Approach 2: Expand Around Center (Most Practical)

This clever trick involves expanding around each possible palindrome "center" in the string.

Article content

  • Time Complexity: O(n²)
  • Space Complexity: O(1)
  • Status: ✅ Great for real-world use and interviews

🚀 Approach 3: Manacher’s Algorithm (O(n) Time)

Manacher's Algorithm is the fastest known algorithm for this problem. It preprocesses the input string by inserting separators (#) to treat even and odd length palindromes uniformly.

Article content

🔍 Why Manacher’s is O(n) Despite Nested Loops

It looks like we have a for loop with a while inside. So why isn’t it O(n²)?

✅ Because:

  • Each expansion (inside while) increases the palindrome radius at most once per character.
  • The total number of such expansions across all iterations is bounded by O(n).

This is a classic example where amortized analysis reveals a linear runtime.

🏁 Final Thoughts

  • Use Expand Around Center in interviews and production—it’s clean, fast, and intuitive.
  • Use Manacher’s Algorithm if you need absolute speed or want to flex algorithmic muscle.
  • Understand the trade-offs and pick the right tool for the job.

To view or add a comment, sign in

More articles by Carlos Santana Roldán

  • #13 - Roman to Integer - Leetcode

    🧩 Problem Statement You are given a string containing a Roman numeral, and your task is to convert it to an integer…

  • #12 - Integer to Roman - Leetcode

    Roman numerals are an ancient way of representing numbers using combinations of letters from the Latin alphabet: . Each…

  • #11 - Container With Most Water - Leetcode

    One of the most popular interview problems on LeetCode is “Container With Most Water.” It’s not only a great test of…

  • #10 - Regular Expression Matching - Leetcode

    🔍 Problem Overview The Regular Expression Matching problem (Leetcode #10) asks you to implement a simplified version…

  • #9 - Palindrome Number – LeetCode

    Checking whether a number is a palindrome is a classic coding interview question that tests your ability to think…

  • #8 - String to Integer (atoi) - Leetcode

    📘 Problem Summary Implement a function that converts a string to a 32-bit signed integer similar to the C/C++ function…

  • #7 - Reverse Integer - Leetcode

    📌 Problem Statement Given a signed 32-bit integer , return with its digits reversed. If reversing causes the value to…

  • #6 - Zigzag Conversion - Leetcode

    Zigzag Conversion is a popular coding problem that appears in technical interviews and algorithm challenges. In this…

  • 🔍 Binary Search Explained Like You're 7

    Let’s face it, binary search sounds complicated, but once you understand the trick behind it, it’s actually pretty fun.…

    1 Comment
  • #4 - Median of Two Sorted Arrays - Leetcode

    Problem #4 on LeetCode is called “Median of Two Sorted Arrays”, and it might sound scary at first… But today we’ll…

Insights from the community

Others also viewed

Explore topics