Week 1 · Day 6 of 42

Two Pointer: Expanding From Center

"Find palindromic substrings" → for every index, expand outward with two pointers (left--, right++) while characters match. Check both odd-length (center = i) and even-length (center = i, i+1).

Sign in to tick off tasks and keep your progress on every device

Two Pointers - Expanding From Center (Palindromes)

Tick the box when you have studied this pattern

How to recall this pattern

"Find palindromic substrings" → for every index, expand outward with two pointers (left--, right++) while characters match. Check both odd-length (center = i) and even-length (center = i, i+1).
babadcenter← LR →
  • For each index (and each pair of adjacent indices), expand outward while characters match.
  • Handles both odd-length and even-length palindromes.
  • Track the longest palindrome found so far by comparing lengths.
  • Time: O(n²) worst case, Space: O(1). Can be improved to O(n) with Manacher's algorithm.
Python template and worked solutions

Solve these

Try each one before reading a solution. Name the pattern out loud first.

Spotted a mistake on this page?

Wrong formula, broken animation or confusing wording. Tell us and we will fix it.