algoblazerEarly Access
LearnProblemsStudy GroupsMock InterviewTrail BlazeLeaderboard
Log inSign up

All problems

‹ Back to map
1 shown · 0/332 solved

Count Distinct Palindromic Substrings

GoldCommunity Beta
Asked atChubb
Solve problem →
2000ms256MBAdded Oct 17, 2026

Given a string s of lowercase English letters, return the number of distinct non-empty substrings of s that are palindromes.

A substring is a contiguous part of s. A palindrome reads the same forwards and backwards. A palindrome that occurs at several positions in s is counted once.

Examples

Example 1

Input: s = "aabaa"
Output: 5
Explanation: The distinct palindromic substrings are "a", "b", "aa", "aba", and "aabaa".
"aa" occurs twice and "a" occurs four times, but each is counted once.

Example 2

Input: s = "abc"
Output: 3
Explanation: Only the single letters "a", "b", and "c" are palindromes.

Constraints

  • 1≤1 \leq1≤ s.length ≤105\leq 10^5≤105
  • s consists of lowercase English letters.

Details

Solved by1 person
Time limit2000ms
Memory256MB
AddedOct 17, 20263 hours ago