Count Substrings That Include a,b and c

Quick Overview

Count Substrings That Include a,b and c. The string s uses only a,b,c. Return the number of contiguous substring occurrences containing at least one of each of these three letters.

Count Substrings That Include a,b and c

Company: Google

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Other

### Problem The string s uses only a,b,c. Return the number of contiguous substring occurrences containing at least one of each of these three letters. Equal substring text at different positions counts separately. ```cpp class Solution { public: int numberOfSubstrings(string s); }; ``` ### Examples **Example 1:** ```text Input: s = "abca" Output: 3 Explanation: The qualifying intervals contain abc,abca,and bca. ``` **Example 2:** ```text Input: s = "aaaa" Output: 0 Explanation: No substring contains b or c. ``` ### Constraints - s has length 3 to 50000. - Its characters are only a,b or c. ### Contract considerations All three letters are required; having at least three characters is insufficient by itself. ```hint Read the boundary condition Count substring occurrences by position. Include strings missing a required letter and strings with repeated copies of all three letters. ``` ### Boundary reminders All three letters are required; having at least three characters is insufficient by itself.

Overview: Count Substrings That Include a,b and c. The string s uses only a,b,c. Return the number of contiguous substring occurrences containing at least one of each of these three letters.

Google logo
Google
Source window: 0–6 months · as of Oct 21, 2026
mediumSoftware EngineerOtherCoding & Algorithms
0
0

Problem

The string s uses only a,b,c. Return the number of contiguous substring occurrences containing at least one of each of these three letters. Equal substring text at different positions counts separately.

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

Examples

Example 1:

Input: s = "abca"
Output: 3
Explanation: The qualifying intervals contain abc,abca,and bca.

Example 2:

Input: s = "aaaa"
Output: 0
Explanation: No substring contains b or c.

Constraints

  • s has length 3 to 50000.
  • Its characters are only a,b or c.

Contract considerations

All three letters are required; having at least three characters is insufficient by itself.

Boundary reminders

All three letters are required; having at least three characters is insufficient by itself.