Count how many substrings of a string are palindromes.
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 <= 1000sconsists of lowercase English letters.
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.