intermediate9 min read·Updated October 7, 2026
Longest Palindromic Substring Explained: Tracing 'cbbd' & 'babad'
Master the Longest Palindromic Substring problem. Walk through the center expansion model using 'cbbd' and 'babad', complete with time complexity and edge cases.
By Learnisim AI·Published October 7, 2026
LEARNING ARCSetup → Model in the example → Full trace → Twist → Edge cases → Apply
PREREQUISITES
- Basic string indexing and slicing in any programming language
Why
Why simple string slicing fails for finding the longest palindromic substring
Phase 1: The Substring Trap
Imagine you are handed the string
s = "cbbd". Your goal is to find the longest contiguous block of characters that reads the exact same forwards and backwards. For s = "cbbd", that target is bb. For s = "babad", it is either bab or aba. It sounds simple: why not just generate every possible substring, check if it is a palindrome, and keep the longest one?If a string has length , there are roughly unique substrings. For a tiny string like
babad (), that is 15 candidates. But scale that up to a modest 1,000-character string, and you are suddenly inspecting nearly 500,000 substrings. Checking each one by reversing it takes extra work, pushing a naive brute-force approach to a sluggish runtime. Worse, you waste endless CPU cycles re-checking overlapping regions like aba inside ababa from scratch.We need a method that avoids redundant checks and zeroes in on the true longest palindromic substring without generating every useless fragment.
Model
The Center Expansion Model: Viewing Palindromes from the Inside Out
Phase 2: The Center Expansion Model
When we looked at finding the longest palindromic substring in our working examples like
cbbd and babad, checking every possible start and end index brute-style led to an explosion of redundant checks. Instead of hunting from the outside in, we flip our perspective to look at expansion around centers. Every palindrome is guaranteed to have a precise middle point. For a string of length , how many such middle points exist?Think about the possible axes of symmetry in
cbbd. A palindrome can have an odd length (anchored on a single exact character like the first b in cbbd) or an even length (anchored strictly between two adjacent characters like the double bb in cbbd). If we test every single index as an odd-length center and every gap between and as an even-length center , we cover every possible palindrome topology without missing a single configuration.To see this mapped to our working data, consider index 1 in
cbbd (the first b). If we treat it as an odd center, our left and right pointers both start at index 1. Inspecting outwards: s[1] matches s[1] (b equals b), but expanding further left hits s[0] (c) and right hits s[2] (b), so c != b and the expansion stops. But what about the even center between index 1 and index 2 (bb)? Here, left starts at 1 (b) and right starts at 2 (b). They match! Expanding outward from there fails, yielding our candidate substring bb of length 2.By treating the string as a sequence of potential centers ( single-character centers and dual-character gaps), we transform a chaotic search space into a systematic sweep. We only expand outward as long as our boundary characters match, recording the maximum length and bounds whenever we beat our previous record.
Worked example
Tracing Center Expansion Step by Step on C-B-B-D
Phase 3:
Let us trace the center expansion algorithm on our working example strings = "cbbd". Our goal is to find any longest palindromic substring by testing every possible center.Given
Strings = "cbbd" of length 4. We maintain two variables for our best result: start = 0 and maxLength = 1, representing the slice s[0:1] ("c") as our initial best palindrome.Steps
We loop through each indexi from 0 to 3, checking both odd-length and even-length centers:1. Index
- Odd center
- Even center
i = 0 (character 'c'):- Odd center
(0, 0): Left and right start at index 0 (s[0] == 'c'). Expand left and right while bounds hold and characters match. No expansion possible. Length = 1. Best remains "c" (length 1).- Even center
(0, 1): Left = 0 ('c'), Right = 1 ('b'). Since s[0] != s[1], expansion fails immediately. Length = 0.2. Index
- Odd center
- Even center
i = 1 (character 'b'):- Odd center
(1, 1): Left = 1 ('b'), Right = 1 ('b'). Expand: check s[0] ('c') and s[2] ('b'). They do not match ('c' != 'b'). Length = 1. Best remains length 1.- Even center
(1, 2): Left = 1 ('b'), Right = 2 ('b'). Since s[1] == s[2], palindrome "bb" is valid! Expand further: Left = 0 ('c'), Right = 3 ('d'). s[0] != s[3], so expansion stops. Length = . Since , update start = 1 and maxLength = 2.3. Index
- Odd center
- Even center
i = 2 (character 'b'):- Odd center
(2, 2): Left = 2 ('b'), Right = 2 ('b'). Expand: Left = 1 ('b'), Right = 3 ('d'). s[1] != s[3]. Length = 1. Best remains length 2.- Even center
(2, 3): Left = 2 ('b'), Right = 3 ('d'). s[2] != s[3]. Length = 0.4. Index
- Odd center
- Even center
i = 3 (character 'd'):- Odd center
(3, 3): Single character 'd'. Length = 1.- Even center
(3, 4): Out of bounds.Result
The search completes. The final best slice corresponds tostart = 1 and maxLength = 2, extracting s[1:3], which yields the expected result: "bb".Try this: Trace the center expansion algorithm manually for s = "babad". List the center indices (both odd and even) that successfully yield the longest palindromic substring "bab" or "aba".
Practice
Test Your Center Expansion Mechanics on B-A-B-A-D
Phase 4:
Now it is time to put the center expansion model to work on our second canonical input:
s = "babad". Recall that in the previous phase, we traced s = "cbbd" and found that the even center between indices 1 and 2 yielded bb. Here, you will trace s = "babad" yourself to see how odd and even centers compete and why both lengths can appear as valid answers.Take out a notepad or trace mentally through every center index from
i = 0 to i = n - 1. Check both odd expansions (where left and right start at i) and even expansions (where left starts at i and right starts at i + 1). Keep a running tracker of the longest valid palindromic substring found so far, updating your best start and end indices whenever an expansion exceeds the previous maximum length.The Task
Given the string
s = "babad", determine the final longest palindromic substring recorded by the center expansion algorithm after checking all possible centers.Question:
When running the center expansion algorithm on
When running the center expansion algorithm on
s = "babad", what are the lengths and starting positions of the palindromes discovered at center index i = 1 (odd center b at index 1) and the even center between index 1 (a) and index 2 (b), and which substring is ultimately returned as the final answer?Apply
Transferring Center Expansion to Overlapping and Alternating Structures
Phase 5: Transfer
Now that we have traced
cbbd to find bb and validated babad to find bab or aba, we can examine how this exact center-expansion pattern transfers to more challenging variations. Consider a string entirely composed of identical characters like aaaa, or a string with alternating patterns where every single-character and two-character center could potentially expand across the entire length. In aaaa, the center at index 1 expands outward through indices (1,2), then (0,3), immediately yielding length 4 (aaaa). The fundamental rule remains identical: every index acts as a dual anchor for odd and even expansions. However, the runtime behavior changes because maximum possible overlaps cause the inner loops to run closer to their theoretical limit per center instead of terminating early on mismatching characters like d in cbbd.When you encounter new substring or subsegment problems where solutions mirror around a pivot—such as finding palindromic partitions or counting substring symmetries—do not rebuild a fresh brute-force validator. Instead, check whether your state space can be decomposed into discrete centers. If a structure allows left-right boundary checks that grow monotonically outward, center expansion transfers directly with zero extra space overhead.
FAQ
What is the result of finding the longest palindromic substring in 'cbbd'?
For the string 'cbbd', the longest palindromic substring is 'bb', found by expanding outwards from the center between the two adjacent 'b' characters.
Why does simple string slicing fail for this problem?
Generating all possible substrings takes O(n^2) space and checking each one for palindromic symmetry takes O(n) time, leading to an inefficient O(n^3) brute-force approach.
What is the time and space complexity of the center expansion approach?
Center expansion runs in O(n^2) time in the worst case (since there are 2n-1 possible centers) and uses O(1) auxiliary space.
How do you handle both odd and even length palindromes?
You must check centers of length 1 (single character, for odd-length palindromes like 'aba') and centers of length 2 (between two characters, for even-length palindromes like 'bb').