Skip to content
AI360Xpert
Beta
Difficulty: Medium1-D Dynamic Programming

Palindromic Substrings

Problem in Plain English

A substring reads the same forward and backward when its outer characters match all the way inward. Count every contiguous slice of the input string that has this property.

Problem Statement

Given a string s, return the number of palindromic substrings in it. A string is a palindrome when it reads the same backward as forward, and a substring is a contiguous block of characters inside s.

Every single character counts as a palindrome of length 1, and overlapping substrings at different positions are counted separately even when they look identical.

Return the total count across all start and end positions.

Constraints

  • 1 <= s.length <= 1000
  • s consists of lowercase English letters.

Examples

Example 1

Input
s = "abc"
Output
3

The three single characters at indices 0, 1, and 2 are palindromes. The other candidates ab, bc, and abc have unequal outer letters, so the count is 3.

Example 2

Input
s = "aaa"
Output
6

There are three occurrences of a, two occurrences of aa, and one occurrence of aaa. They overlap and repeat the same text, but each start/end pair counts separately: 3 + 2 + 1 = 6.

Example 3

Input
s = "z"
Output
1

The only non-empty substring is z itself. Its odd center adds one, and the gap after it is already outside the string.

Example 4

Input
s = "abba"
Output
6

Four single-character occurrences count, plus bb at indices 1..2 and abba at 0..3. Both longer palindromes share the middle gap, showing why even centers and every successful expansion must be counted.

Intuition

Count occurrences, not distinct text: the three one-letter substrings in aaa count separately because their positions differ. A palindrome mirrors around a character for odd lengths or around a gap for even lengths. In Example 1, abc contributes only its three character centers; every neighboring gap fails immediately. Enumerating all endpoint pairs works, but growing each center shares the inner palindrome instead of repeatedly checking it.

Approaches

Brute Force Check

Solution Details

Reveal Brute Force Check: intuition, complexity, and code

Hints

Hint 1
A palindrome has a unique middle: a character for odd lengths, or a gap for even lengths.
Hint 2
For one middle, count every successful outward layer rather than only the widest palindrome.
Hint 3
Expand from both (i, i) and (i, i + 1) at every index. Count while bounds hold and characters match; stop at the first failure.

Edge Cases

  • A single character like z contributes exactly one odd palindrome and no even palindrome.
  • For all identical letters, every interval qualifies: the count is n(n+1)/2n(n+1)/2, or 500500 at the length limit 1000.
  • All distinct letters like abc contribute exactly n single-character occurrences.
  • An even middle like the gap in abba can contribute multiple palindromes (bb and abba); counting only character centers misses both.
  • A mismatch after a successful inner palindrome stops that center, but other centers must still be processed.

Common Mistakes and Interview Tips

  • Deduplicating by substring text: aaa contains six occurrences, even though there are only three distinct palindrome texts.
  • Expanding only around characters misses every even-length palindrome, such as bb in abba.
  • Incrementing once per center instead of once per successful layer misses nested palindromes around the same middle.
  • Indexing before checking bounds is especially dangerous in Python, where a negative index wraps to the end instead of raising immediately.
  • Slicing or reversing every candidate changes the allocation behavior; use endpoint indices if claiming constant auxiliary space.
  • Interview tip: justify completeness using a unique center and radius, and distinguish this count from finding only the longest palindrome.

Key Takeaway

When each valid interval has a unique center, enumerate centers and grow by one layer at a time. A preserved inner invariant replaces repeated full checks; counting every successful radius counts occurrences, while retaining only the largest radius solves a different objective.