Count Palindromic Substrings

Quick Overview

Practice counting every contiguous palindromic substring by position, including single characters and duplicate text at different indexes.

Count Palindromic Substrings

Company: Goldman Sachs

Role: Data Scientist

Category: Coding & Algorithms

Difficulty: easy

Interview Round: Online Assessment

# Count Palindromic Substrings Implement `count_palindromic_substrings(text: str) -> int`. Return the number of contiguous substrings of `text` that read the same forward and backward. Count occurrences by their start and end positions, so equal text at different positions counts separately. Every single-character substring is a palindrome. ## Valid Input Domain - `text` contains lowercase English letters. ## Constraints - `0 <= text.length <= 5,000` - The answer fits in a signed 64-bit integer. ## Public Examples ### Example 1 Input: `"abc"` Output: `3` Only the three single-character substrings are palindromes. ### Example 2 Input: `"aaa"` Output: `6` The palindromic occurrences are three of length one, two of length two, and one of length three. ```hint Choose palindrome centers Odd- and even-length palindromes have different kinds of centers but can be counted with the same expansion idea. ```

Quick Answer: Practice counting every contiguous palindromic substring by position, including single characters and duplicate text at different indexes.

|Home/Coding & Algorithms/Goldman Sachs
Goldman Sachs logo
Goldman Sachs
Sep 2, 2026
easyData ScientistOnline AssessmentCoding & Algorithms
2
0

Count Palindromic Substrings

Implement count_palindromic_substrings(text: str) -> int.

Return the number of contiguous substrings of text that read the same forward and backward. Count occurrences by their start and end positions, so equal text at different positions counts separately. Every single-character substring is a palindrome.

Valid Input Domain

  • text contains lowercase English letters.

Constraints

  • 0 <= text.length <= 5,000
  • The answer fits in a signed 64-bit integer.

Public Examples

Example 1

Input: "abc"

Output: 3

Only the three single-character substrings are palindromes.

Example 2

Input: "aaa"

Output: 6

The palindromic occurrences are three of length one, two of length two, and one of length three.

Submit Your Answer to Earn 20XP

Sign in to leave a comment

Loading comments...