Count Palindrome Occurrences among Contiguous Substrings

Quick Overview

Count Palindrome Occurrences among Contiguous Substrings. Return the number of nonempty contiguous substrings of s that read the same forward and backward.

Count Palindrome Occurrences among Contiguous Substrings

Company: Amazon

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Other

### Problem Return the number of nonempty contiguous substrings of s that read the same forward and backward. Different start/end ranges count separately even when their text is identical. ```cpp class Solution { public: int countSubstrings(string s); }; ``` ### Examples **Example 1:** ```text s = "abba" Output: 6 Explanation: Four single letters, "bb", and "abba" qualify. ``` **Example 2:** ```text s = "aaaa" Output: 10 Explanation: Every one of the 4+3+2+1 substrings is palindromic. ``` ### Constraints - The string contains 1..1000 English lowercase characters. ### Clarifying Questions Include single-character and even-length palindromes and count equal text at different positions separately. ```hint Read the boundary condition What two kinds of centers can a palindrome have? ``` ### Contract considerations Include single-character and even-length palindromes and count equal text at different positions separately.

Overview: Count Palindrome Occurrences among Contiguous Substrings. Return the number of nonempty contiguous substrings of s that read the same forward and backward.

Amazon logo
Amazon
Sep 11, 2026
mediumSoftware EngineerOtherCoding & Algorithms
0
0

Problem

Return the number of nonempty contiguous substrings of s that read the same forward and backward. Different start/end ranges count separately even when their text is identical.

class Solution {
public:
    int countSubstrings(string s);
};

Examples

Example 1:

s = "abba"
Output: 6
Explanation: Four single letters, "bb", and "abba" qualify.

Example 2:

s = "aaaa"
Output: 10
Explanation: Every one of the 4+3+2+1 substrings is palindromic.

Constraints

  • The string contains 1..1000 English lowercase characters.

Clarifying Questions Guidance

Include single-character and even-length palindromes and count equal text at different positions separately.

Contract considerations

Include single-character and even-length palindromes and count equal text at different positions separately.