Count Palindromic Substrings.
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
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
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.
Expected Output