Skip to main content
Back to problems
Leetcode
Medium
Strings
Dynamic Programming
Google
Meta
Palindromic Substrings

Count how many substrings of a string are palindromes.

Acceptance 100%
Problem Statement

Given a string s, count how many of its substrings are palindromes. A substring is a contiguous sequence of characters, and a palindrome reads the same forward and backward.

Your task is to return the total number of palindromic substrings in s.

A substring may appear multiple times at different positions and should be counted separately.

Input Format

  • A single string s.

Output Format

  • Return the number of palindromic substrings in s.

Constraints

  • 1 <= s.length <= 1000
  • s consists of lowercase English letters.
Examples
Sample cases returned by the problem API.

Example 1

Input

s = "abc"

Output

3

Explanation

The palindromic substrings are "a", "b", and "c".

Example 2

Input

s = "aaa"

Output

6

Explanation

The palindromic substrings are "a" (3 times), "aa" (2 times), and "aaa" (1 time).

Premium problem context

Unlock deeper context for this problem

Premium adds guided hints, editorial links, similar variants, discussion resources, and concept maps so you can understand why a problem matters, not just solve it once.

Guided hints
Editorial and discussion links
Concept map and variants
Sign in to unlock
Track your progress
Sign in to bookmark this problem, save notes, and manage its revision plan.