Palindromic Substrings
Given a string s, return the number of palindromic substrings in it.
A string is a palindrome when it reads the same backward as forward. A substring is a contiguous sequence of characters within the string.
Examples
Input: "abc"
Output: 3
Input: "aaa"
Output: 6
Hints
Consider that a palindrome can have either a single character as its center (odd-length) or two identical characters as its center (even-length).
For each possible center (character or gap between characters), expand outward while the characters on both sides match, counting each valid palindrome found.
Optimize by recognizing that each palindrome expansion can be reused to avoid redundant checks, ensuring the solution runs in O(n²) time with O(1) space.
Related Problems
Palindromic Substrings
Given a string `s`, return the number of **palindromic substrings** in it.