Problem · String
Count Palindromic Substrings
Learn this problemProblem statement
Given a non-empty string s, return the number of its palindromic substrings.
A palindrome reads the same from left to right and from right to left. Count substrings by occurrence, so equal text at different positions counts separately. Every one-character substring is a palindrome.
Function
countPalindromicSubstrings(s: String) → intExamples
Example 1
s = "abc"return = 3The three one-character substrings are palindromes.
Example 2
s = "aaa"return = 6The palindromic occurrences are three copies of a, two copies of aa, and one copy of aaa.
Example 3
s = "abba"return = 6The four individual characters, bb, and abba are palindromes.
Constraints
1 <= s.length <= 2000scontains only lowercase English letters.