Arun Pandian M

Arun Pandian M

Android Dev | Full-Stack & AI Learner

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

https://storage.googleapis.com/lambdabricks-cd393.firebasestorage.app/expand_around_center.svg?X-Goog-Algorithm=GOOG4-RSA-SHA256&X-Goog-Credential=firebase-adminsdk-fbsvc%40lambdabricks-cd393.iam.gserviceaccount.com%2F20260225%2Fauto%2Fstorage%2Fgoog4_request&X-Goog-Date=20260225T032813Z&X-Goog-Expires=3600&X-Goog-SignedHeaders=host&X-Goog-Signature=41b1641cd635262993fe448a7b8b6f494a53e5f9a3af38016f184e97826fe34976d779001f69a72f6c0d5ecf5f4c0a25d387f5e6d29d6b2d2351a6d5e89cb68bf8df8ed949cb910ac42fe30aee503b4f94ffb94a4701e3d2d2d8371865f3e852f063c098feaf4f73366c798cb2b4a41d4545e9a9fffc40c0efabf3e63459fa28cbecbf50ec336d944f28aafd4eecac5099ab266eaa3d642685be925b8513138ac735fe8d89caa5c2f743f517cfb69942dbce60302cae05c2e8ba715d824f99fb00b2a7fcfd930c11caf78ba2213a1e71125bf8a933b8426e5884d3dc2d4432939422ac8a7a127cb1da598c09c999d8a46a6c10a78024047e7e0a3b1105caf488

Expand Around Center is not just about palindromes. It is about detecting symmetry around a balance point.

In any symmetric structure:

  • There exists a central axis
  • The structure mirrors itself on both sides
  • You can verify symmetry by expanding outward
  • 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:

  • Measure every window combination
  • Compare random corners
  • 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:

  • You compare every possible segment
  • You recompute symmetric checks repeatedly
  • With center expansion:

  • You only expand valid symmetric regions
  • You avoid redundant work
  • You control exploration
  • This reduces brute-force behavior.

    Where This Pattern Applies

    Not just palindromes. It applies when:

  • You need to detect symmetric regions
  • You are searching for mirrored structures
  • You want to measure radius around a balance point
  • A problem mentions center or mirror
  • 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
      ↑ center

    Expands 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:

  • We only expand valid symmetric regions.
  • We stop immediately on mismatch.
  • We avoid redundant work.
  • 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
    	•	center

    No 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:

  • Dynamic Programming on symmetric substrings
  • Manacher’s Algorithm (optimized symmetric reuse)
  • 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.

    #ProblemSolving#AlgorithmIntuition#TimeComplexity#LearnInPublic#ExpandAroundCenter#SymmetryDetection#PalindromePattern#TwoPointerTechnique#StringAlgorithms#DSAPatterns#AlgorithmDesign#CodingPatterns#DataStructures#SpaceComplexity