← Visualizers
Two Pointers · expand around center LeetCode 647

Palindromic Substrings / visualized

This is literally the same loop as LeetCode 5 — same 2n−1 centers, same two pointers starting together and moving apart, same while (s[l] == s[r]). Only the accumulator changes: LeetCode 5 keeps a maximum, this one keeps a counter. That single-line swap is the whole reason both problems sit in the syllabus, and it works because the expansion has a hidden bijection built into it: every palindromic substring has exactly one center, so incrementing once per successful widen counts each substring exactly once, with no dedup pass and no set. The n−1 gap centers are still the part people drop — forget them and "abba" answers 4 instead of 6.

Execution

idle
s
centers
0count
Press Run to begin.
0 / 0
Speed

Java · running line

current center l — expanding left r — expanding right counted palindrome mismatch that stops the center
O(n²) time · O(1) extra space  ·  every palindromic substring is generated by exactly one of the 2n−1 centers, so count++ per successful expansion is already deduplicated. Swap that line for if (r-l+1 > bestLen) and you have LeetCode 5.