Expand Around Center — A Symmetry Discovery Pattern
When a structure is symmetric, the fastest way to verify it is not from the edges — it’s from the balance point. Instead of scanning everything, you start at the center and grow outward.
That’s the idea behind the Expand Around Center pattern.
Core Concept
Expand Around Center is not just about palindromes. It is about detecting symmetry around a balance point.
In any symmetric structure:
The key idea is:
Symmetry is defined by its center — not by its ends.
If you know the center of a balanced structure, you can grow outward and verify alignment step by step.
Behavior Model
The behavior is mechanical and predictable:
1.Choose a potential center
2.Compare left and right sides
3.Expand while they match
4.Stop when symmetry breaks
Formally:
left ← center → right
While:
left element == right element
Expand.
This is controlled symmetric expansion.
Real-Time Analogy
Imagine standing in front of a perfectly symmetrical building.
You don’t:
Instead:
1.You stand at the central entrance
2.Compare left wing and right wing
3.Move outward
4.Stop when imbalance appears
The symmetry is defined by the central axis. That’s Expand Around Center.
Why This Pattern Exists
Without this approach:
With center expansion:
This reduces brute-force behavior.
Where This Pattern Applies
Not just palindromes. It applies when:
Palindromes are just the most common example.
Problem
Longest Palindromic Substring
Given a string s, return the longest substring that is symmetric.
Example
Input:
"bdabadd"Output:
"abada"Because:
a b a d a
↑ centerExpands symmetrically outward.
Flow (Step-by-Step)
Start
|
v
If string is empty:
return ""
|
v
Initialize start = 0
Initialize end = 0
|
v
For each index as center:
|
v
Check odd-length expansion
|
v
Check even-length expansion
|
v
If new length > current best:
Update start
Update end
|
v
Continue until last index
|
v
Return substring(start, end+1)Kotlin Implementation
package org.example.twopointerPattern
import kotlin.math.max
fun longestPalindrome(string: String): String {
if (string.isEmpty()) return ""
var start = 0
var end = 0
val length = string.length
for (center in 0..<length) {
val oddLength = palindromeLength(string, center, center)
val evenLength = palindromeLength(string, center, center + 1)
val currentLength = max(oddLength, evenLength)
if (currentLength > end - start) {
start = center - (currentLength - 1) / 2
end = center + currentLength / 2
}
}
return string.substring(start, end + 1)
}
fun palindromeLength(string: String, left: Int, right: Int): Int {
var leftL = left
var rightL = right
while (
leftL >= 0 &&
rightL <= string.length - 1 &&
string[leftL] == string[rightL]
) {
leftL--
rightL++
}
return rightL - leftL - 1
}
fun main() {
println(longestPalindrome("bdabadd"))
}Why This Works
Because a palindrome is defined by symmetry around its center. Instead of checking every substring:
Time & Space Complexity
Time Complexity: O(n²)
The string is scanned using possible symmetry centers.
For a string of length n, we attempt expansion from each index (including gaps between characters). For every center, we expand outward as long as characters match.
In the worst case (for example, "aaaaaa"), every expansion can grow nearly the full length of the string.
Since we perform expansion for each center and each expansion can take linear time, the total work becomes quadratic. Even though each expansion stops immediately when mismatch occurs, the worst-case total expansions across all centers lead to O(n²) time.
Space Complexity: O(1)
The algorithm does not allocate any additional data structures. It only maintains a few integer variables:
• start
• end
• left
• right
• centerNo extra arrays, no dynamic storage, no recursion stack. The palindrome is determined using index calculations alone. Therefore, the space usage remains constant — O(1).
Conceptual Bridge
Expand Around Center is a stepping stone.
It leads to:
Final Takeaway
Expand Around Center teaches a structural insight:
When symmetry exists, start at the balance point and grow outward.
It’s not just about palindromes. It’s about understanding how symmetry behaves in discrete structures. Once you see problems as symmetry detection problems, this pattern becomes natural.
