Return the longest continuous palindrome in a string.
Start with the concrete trace below. It shows the state the algorithm must carry as it runs.
Problem trace
Longest Palindromic Substring: Every odd or even palindrome is discovered by expanding its unique center; update the saved range only when an expansion is strictly longer.
Input and goalReturn the longest continuous palindrome in a string.
Initialize best rangeFor text babad, best_left = best_right = 0, so the initial saved palindrome is b.
best [0,0]b0a1b2a3d4
inputbabadbestRange[0,0] -> b
Process middle 0Odd expansion accepts b but ties the saved length; even expansion compares b with a and stops.
L=0middle=0b0a1b2a3d4
inputbabadcallsexpand(0,0); expand(0,1)expansionsodd [0,0]=b; even b!=abestRange[0,0] -> b
Process middle 1Odd expansion accepts a, then bab. Width 2 is greater than saved width 0, so best becomes [0,2]; even a!=b.
L=0b0middle=1a1R=2b2a3d4
inputbabadcallsexpand(1,1); expand(1,2)expansionsodd a -> bab -> bounds stop; even a!=bbestRange[0,2] -> bab
Process middle 2Odd expansion accepts b, then aba. Its width 2 ties bab, so the strict greater-than branch keeps bab; even b!=a.
b0L=1a1middle=2b2R=3a3d4
inputbabadcallsexpand(2,2); expand(2,3)expansionsodd b -> aba -> b!=d; even b!=abestRange[0,2] -> bab
Process middle 3Odd expansion accepts a then b!=d; even compares a with d and stops. Neither candidate beats bab.
b0a1b2L=3middle=3a3d4
inputbabadcallsexpand(3,3); expand(3,4)expansionsodd a -> b!=d; even a!=dbestRange[0,2] -> bab
Process middle 4 and returnOdd expansion accepts d and reaches the boundary; even starts out of bounds. Slice [0:3] returns bab.
L=0b0a1R=2b2a3middle=4d4
inputbabadcallsexpand(4,4); expand(4,5)expansionsodd d -> boundary; even right=5 out of boundsbestRange[0,2] -> babresultbab
Recognize it
The answer is a contiguous palindrome, whose symmetry guarantees one unique odd character center or even gap center.
Keep true
Within expand(left,right), text[left:right+1] is palindromic before the pointers move outward. After each completed center, the saved range is the longest palindrome seen at any processed center.
Reuse it
When validity is symmetric around a center, enumerate both center types and expand until the invariant breaks; change only the aggregation to find a longest value, count all values, or validate radii.
Read it this way: For text babad, best_left = best_right = 0, so the initial saved palindrome is b. Step through the frames to watch the state change. The last frame shows the answer or the stopping condition.
Pattern: Expand from every center.
Simple idea: Every palindrome has one center character or a gap between two center
characters. Expand both forms at every position and save the longest range.
def longest_palindrome(text: str) -> str: best_left = best_right = 0 def expand(left: int, right: int) -> None: nonlocal best_left, best_right while left >= 0 and right < len(text) and text[left] == text[right]: if right - left > best_right - best_left: best_left, best_right = left, right left -= 1 right += 1 for middle in range(len(text)): expand(middle, middle) expand(middle, middle + 1) return text[best_left : best_right + 1]