Given a string s, return the number of palindromic substrings in it. A substring is a contiguous sequence of characters within the string.
s = "abc"3count = 0i, call expand(i,i) and expand(i,i+1)expand: while s[l]==s[r], increment count, then l--, r++countEvery palindrome has a center. Listing odd and even centers covers all palindromic substrings exactly once per center expansion path.
Why count each step? When s[l..r] is a palindrome, that contiguous substring is valid. The inner while discovers nested palindromes from the center outward; each layer is a distinct substring.
vs. LPS (LC 5): Longest palindrome tracks only the maximum length; here we add 1 to count for every palindrome we see.