ExamAdda Logo

Count Palindromic Substrings.

Medium

Given a string s, return the total number of palindromic substrings in it.

A substring is a contiguous sequence of characters. A palindrome reads the same forward and backward. Count each occurrence separately, even if multiple substrings contain the same characters.

Example 1

Input

abc

Output

3

Explanation

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

Example 2

Input

aaa

Output

6

Explanation

The palindromic substrings are "a", "a", "a", "aa", "aa", and "aaa". Each occurrence is counted separately.

Constraints

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

Hints:

Hint 1

Every individual character is a palindrome.

Hint 2

Consider each character and each gap between adjacent characters as a possible center. Expand outward while the characters match, counting each palindrome found.

Author & Technical Reviewer

Chatla Jithin Sri Surya

Written by: Chatla Jithin Sri Surya

Chatla Jithin Sri Surya

Technically reviewed by: Chatla Jithin Sri Surya

Published

Oct 27, 2026

Last updated

Oct 27, 2026

Last technically reviewed

Oct 27, 2026

Content Verification Methodology

Definitions and complexity claims were checked against authoritative computer-science references. Code examples were compiled and tested with standard, boundary and edge-case inputs.

Report a correction:editorial@examadda.org
Auto
Loading editor...
Input

Expected Output