Skip to content
DeepForgeby svx
All problems
EasyAlgorithmsal-180

Count Palindromic Substrings

Problem

Count all palindromic substrings of s, including single characters and overlapping occurrences.

Expand around every center, considering both odd-length and even-length palindromes, and add one for each successful expansion step. An empty string has zero palindromic substrings.

Hint

A palindrome of length L contributes one to the count for every center expansion.

Test cases

case 1
in: ["abc"]
expected: 3
case 2
in: ["aaa"]
expected: 6
case 3
in: [""]
expected: 0
case 4
in: ["a"]
expected: 1
case 5
in: ["aba"]
expected: 4
Starter codePython
def count_palindromic_substrings(s):
    # Your code here
    pass

5 test cases · run your code against them in the editor