Shubham Sourabhthe room · director's cutplain résumé
Room 04 · The Grind — 1-D Dynamic Programming

Longest Palindromic Substring

MEDIUM✓ CHIP-TIMEDLC #5 — FULL STATEMENT

The briefingthe full drill, in my own words

The drill: Somewhere inside a string sits its longest run that reads the same forwards and backwards. Find that substring.

A string arrives, and somewhere inside it sits a contiguous run of characters that reads identically forwards and backwards — a palindrome.

Several different substrings might tie for the longest palindrome; any one of the longest is an acceptable answer, not a specific one among the ties.

The task is to return that substring itself — the actual run of characters, not its length or its position in the string.

  • string length can run into the low thousands
  • characters can include letters, digits, or other symbols
  • single characters count as palindromes of length one
  • when several longest palindromes tie, any one of them is acceptable
EX 01
s = "a"
→ "a"
minimum size, single character
EX 02
s = "xx"
→ "xx"
whole string is the palindrome
EX 03
s = "level"
→ "level"
whole string, odd length

The hintstake only what you need

HINT 1 THE NUDGE

Testing every substring for the palindrome property costs a scan per candidate on top of the count of candidates — where is the redundant work hiding?

HINT 2 THE STRUCTURE

A palindrome is built outward from its center — a single character, or a gap between two — and stops growing the instant its two ends disagree. Every palindrome has exactly one center.

HINT 3 ONE STEP FROM THE ANSWER

Walk every possible center (n single-character centers, n-1 between-character centers), expand outward while the ends match, and keep the widest span you've seen.

Coach's boardthe pattern, step by step

THE CENTER EXPANSIONPATTERN · EXPAND AROUND CENTERs = "banana"
b
a
n
a
n
a
BEST WINDOW SO FAR
— empty —
STEP 1

Every palindrome grows from a center — a single character or a gap. Walk each one, expanding while the ends still match.

STEP 1 / 7 · ← → WORK TOO

The splitstwo paces, two languages

grind/longest-palindromic-substring.py — RACE PACE
LANG ▸
PACE ▸
class Solution:
    def longestPalindrome(self, s: str) -> str:
        n = len(s)

        def expand(l: int, r: int) -> tuple:
            while l >= 0 and r < n and s[l] == s[r]:
                l -= 1
                r += 1
            return l + 1, r - 1                  # last valid window

        start, end = 0, 0
        for i in range(n):
            l1, r1 = expand(i, i)                 # odd-length center
            if r1 - l1 > end - start:
                start, end = l1, r1
            l2, r2 = expand(i, i + 1)              # even-length center
            if r2 - l2 > end - start:
                start, end = l2, r2
        return s[start:end + 1]
TIME O(N^2)SPACE O(1)PYTHON · RACE PACE · 19 LN

✓ CHIP-TIMEDALL 4 SOLUTIONS RAN GREEN AGAINST SELF-AUTHORED CASES IN CI · JDK 21 · CPYTHON 3.12 · NOTHING PUBLISHES RED