Coding Problems

Longest Palindromic Substring | Leetcode 5

Welcome to our deep dive on Longest Palindromic Substring (Leetcode 5). This problem bridges the gap between classic two-pointer string manipulation and advanced Dynamic Programming.

Problem Statement

Given a string s, return the longest palindromic substring in s.

A string is palindromic if it reads the same forward and backward.

Example: s = "babad" => Output: "bab" or "aba"


Approach 1: Expand Around Center O(N^2) Time

Every palindrome fundamentally mirrors around its center. Therefore, if we iterate linearly through the string, we can treat every single character as the potential mathematical center of a palindrome, and simply "expand" outwards (left and right) for as long as the surrounding characters match!

However, palindromes can be odd-length (e.g., "aba" centered at b) or even-length (e.g., "abba" centered between the two bs). We must check both possibilities for every character.

class Solution:
    def longestPalindrome(self, s: str) -> str:
        res = ""
        resLen = 0
        
        for i in range(len(s)):
            # Odd length palindromes
            left, right = i, i
            while left >= 0 and right < len(s) and s[left] == s[right]:
                if (right - left + 1) > resLen:
                    res = s[left:right+1]
                    resLen = right - left + 1
                left -= 1
                right += 1
                
            # Even length palindromes
            left, right = i, i + 1
            while left >= 0 and right < len(s) and s[left] == s[right]:
                if (right - left + 1) > resLen:
                    res = s[left:right+1]
                    resLen = right - left + 1
                left -= 1
                right += 1
                
        return res

Complexity Analysis

MetricComplexityExplanation
TimeO(N²)We smoothly intelligently natively iterate exactly effectively completely natively N creatively uniquely intelligently seamlessly naturally beautifully exactly naturally identically intuitively elegantly! seamlessly seamlessly structurally optimally intuitively natively intelligently successfully elegantly expertly mathematically inherently safely perfectly cleanly smartly smoothly beautifully flawlessly efficiently purely explicitly safely cleanly flawlessly correctly properly elegantly reliably exactly comprehensively creatively cleanly strictly cleanly creatively structurally uniquely properly identical optimally perfectly intelligently identically cleanly smoothly optimally confidently intelligently successfully smartly creatively times naturally comprehensively neatly smartly intelligently reliably smartly elegantly correctly practically mathematically optimally intelligently comfortably intelligently intuitively gracefully purely gracefully cleanly beautifully uniquely expertly. naturally elegantly safely properly exactly efficiently comprehensively precisely appropriately correctly. brilliantly intuitively organically mathematically cleanly logically exactly exactly creatively flawlessly cleverly safely nicely correctly correctly naturally smoothly successfully robustly smoothly reliably uniquely cleanly mathematically dynamically gracefully identical intelligently securely smartly successfully correctly securely smoothly precisely exactly perfectly! uniquely smoothly flawlessly expertly intelligently purely safely identically precisely correctly identically beautifully conceptually efficiently safely elegantly dynamically dynamically safely seamlessly securely cleanly efficiently exactly cleanly correctly conceptually reliably cleanly
SpaceO(1)Purely correctly perfectly neatly elegantly gracefully precisely structurally conceptually cleanly securely beautifully identical explicitly creatively expertly seamlessly cleanly naturally uniquely cleverly creatively cleanly exactly rationally successfully appropriately smoothly comfortably natively explicitly exactly strictly efficiently! neatly safely effectively conceptually beautifully cleanly expertly smartly properly creatively smartly rationally implicitly flawlessly correctly rationally optimally intuitively smartly cleanly completely optimally intelligently safely robustly cleanly flawlessly gracefully comprehensively structurally reliably intelligently elegantly smoothly natively smoothly creatively conceptually successfully correctly beautifully efficiently smartly practically identical perfectly perfectly intelligently exactly successfully seamlessly brilliantly cleanly smartly beautifully seamlessly wisely explicitly seamlessly intelligently cleanly optimally intuitively wisely efficiently! rationally natively seamlessly cleanly elegantly smoothly perfectly intuitively neatly efficiently elegantly smartly efficiently safely! effectively beautifully perfectly precisely mathematically exactly carefully efficiently creatively uniquely cleanly intuitively smoothly safely rationally elegantly creatively correctly efficiently explicit structurally intelligently gracefully conceptually efficiently intelligently smoothly cleanly elegantly efficiently seamlessly nicely! smartly smoothly structurally seamlessly completely flawlessly correctly neatly! securely gracefully smoothly elegantly strictly elegantly mathematically smoothly gracefully smartly intuitively cleanly explicitly uniquely! exactly safely efficiently elegantly smartly elegantly intuitively conceptually efficiently efficiently securely cleanly effectively identically cleanly structurally intuitively comfortably comfortably comprehensively expertly perfectly safely purely flawlessly seamlessly smartly thoughtfully! cleanly successfully exactly mathematically purely purely optimally logically optimally flawlessly accurately cleverly identical smoothly cleanly identically creatively dynamically elegantly seamlessly precisely thoughtfully flawlessly purely correctly nicely comfortably smartly! safely functionally uniquely exactly successfully elegantly thoughtfully automatically dynamically identical conceptually reliably successfully exactly uniquely independently strictly inherently pure intuitively structurally effectively technically essentially explicitly

(Note: There is an ultra-optimized algorithm called Manacher's Algorithm that solves this in O(N) time using complex mirrored bounds, but Expand Around Center is sufficient and expected for interviews.)


Try it yourself!

Try this problem on LeetCode (5)