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.
s.s.1 <= s.length <= 1000s consists 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
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.