Browse curriculum

Palindromic Substrings (LeetCode 647)

Palindromic Substrings (LeetCode 647): count every palindromic substring of a string by expanding two pointers outward from each of its 2n − 1 centres.

Problem Understanding

Palindromic Substrings: given a string s, return how many of its substrings are palindromes. Substrings at different positions count separately, even if they are equal.

Examples: "abc" returns 3 (a, b, c). "aaa" returns 6 (a, a, a, aa, aa, aaa).

Attempt 1: Check Every Substring

There are about n²/2 substrings, and checking each one takes up to n steps: O(n³) time. Most of that work is repeated: checking s[1..5] re-checks s[2..4], which was already known to be a palindrome or not.

The Intuition: Grow Palindromes From Their Centres

A palindrome stays a palindrome when you peel one character off each end, so every palindrome can be grown outward from its centre. Turn that around:

  1. Visit each of the 2n − 1 centres: every character (odd lengths) and every gap between two characters (even lengths).
  2. Put lo and hi on the centre and step outward while s[lo] == s[hi].
  3. Each successful step is one more palindrome: count it.

This is the same walk as Longest Palindromic Substring, counting every palindrome instead of keeping only the longest.

Palindromic Substrings Visualization

2n − 1 CENTRES: CHARACTERS AND GAPS

START

a

0

a

1

a

2

Centre

:

Count

0

counted

None yet

 

count = 0

Next

Centre 0: the character at 0, 'a' (odd lengths)

Free preview: the first 6 of 18 steps

String

The centres are visited in order, alternating character and gap. The palindrome grown so far is filled, lo and hi mark the next comparison, and every palindrome counted is listed beside the figure.

Palindromic Substrings Dry Run

Input: s = "aaa"

Centre Start Palindromes found Count
0 char 0 a 1
1 gap 0-1 aa 2
2 char 1 a, aaa 4
3 gap 1-2 aa 5
4 char 2 a 6

The answer is 6.

Palindromic Substrings LeetCode Solution

Expand around each of the 2n − 1 centres and count every match.

Palindromic Substrings Code
1var countSubstrings = function(s) {
2 let count = 0;
3 const n = s.length;
4 for (let center = 0; center < 2 * n - 1; center++) {
5 let lo = Math.floor(center / 2), hi = lo + (center % 2);
6 while (lo >= 0 && hi < n && s[lo] === s[hi]) {
7 count++;
8 lo--;
9 hi++;
10 }
11 }
12 return count;
13};

Palindromic Substrings in Python, Java, C++, TypeScript and C

Palindromic Substrings in Python
class Solution:
    def countSubstrings(self, s: str) -> int:
        count = 0
        for center in range(2 * len(s) - 1):
            lo, hi = center // 2, center // 2 + center % 2
            while lo >= 0 and hi < len(s) and s[lo] == s[hi]:
                count += 1
                lo, hi = lo - 1, hi + 1
        return count
Palindromic Substrings in Java
class Solution {
    public int countSubstrings(String s) {
        int count = 0, n = s.length();
        for (int center = 0; center < 2 * n - 1; center++) {
            int lo = center / 2, hi = lo + center % 2;
            while (lo >= 0 && hi < n && s.charAt(lo) == s.charAt(hi)) {
                count++;
                lo--;
                hi++;
            }
        }
        return count;
    }
}
Palindromic Substrings in C++
class Solution {
public:
    int countSubstrings(string s) {
        int count = 0, n = s.size();
        for (int center = 0; center < 2 * n - 1; center++) {
            int lo = center / 2, hi = lo + center % 2;
            while (lo >= 0 && hi < n && s[lo] == s[hi]) {
                count++;
                lo--;
                hi++;
            }
        }
        return count;
    }
};
Palindromic Substrings in TypeScript
function countSubstrings(s: string): number {
    let count = 0;
    const n = s.length;
    for (let center = 0; center < 2 * n - 1; center++) {
        let lo = Math.floor(center / 2), hi = lo + (center % 2);
        while (lo >= 0 && hi < n && s[lo] === s[hi]) {
            count++;
            lo--;
            hi++;
        }
    }
    return count;
}
Palindromic Substrings in C
int countSubstrings(char* s) {
    int count = 0;
    int n = strlen(s);
    for (int center = 0; center < 2 * n - 1; center++) {
        int lo = center / 2, hi = lo + center % 2;
        while (lo >= 0 && hi < n && s[lo] == s[hi]) {
            count++;
            lo--;
            hi++;
        }
    }
    return count;
}

Edge Cases & Common Mistakes

  • Forgetting even-length palindromes: checking only character centres misses "bb" in "abba" and returns 5 instead of 6.
  • One character: a single centre and one palindrome; the answer is 1.
  • All characters equal: every substring is a palindrome, so the count is n(n + 1) / 2, the worst case for time.
  • Counting distinct strings instead of positions: the problem counts positions, so equal substrings each count.

Summary

Every palindrome has one centre, so expanding two pointers from all 2n − 1 centres meets each palindrome exactly once. Count each match: O(n²) time, O(1) space.

More practice: DSA interview questions with answers · patterns cheat sheet

The approach, step by step

  1. Pick a centre

    For each centre from 0 to 2n − 2, set lo = centre / 2 and hi = lo + centre % 2.

  2. Expand and count

    While lo and hi are in range and s[lo] == s[hi], add one to the count and move lo left and hi right.

  3. Return the count

    After every centre is done, the count is the answer.

Frequently asked questions

Why are there 2n − 1 centres?

An odd-length palindrome is centred on a character (n choices) and an even-length one on the gap between two characters (n − 1 choices). Together that is 2n − 1 centres, and every palindrome has exactly one.


Is expand around centre better than dynamic programming here?

Both take O(n²) time. Expanding around centres uses O(1) extra space, while the DP table dp[i][j] (is s[i..j] a palindrome?) uses O(n²). Manacher's algorithm counts them in O(n), but is rarely expected in interviews.


Do repeated substrings count more than once?

Yes. Substrings with different start or end positions count separately, so "aaa" has six: three "a", two "aa" and one "aaa".

New to the technique behind this problem? Watch it run step by step:

Two Pointers Visualization

Applying for software engineering roles? Free, no sign-up:

AI resume checker: match your resume to the job description