Skip to content
Decoder

Number of Substrings Containing All Three Characters

Medium

Problem

Given a string s made only of the letters a, b and c, return the number of substrings that contain at least one of each of the three letters.

Example 1

Input
s = "abcabc"
Output
10

Example 2

Input
s = "aaacb"
Output
3
Why
"aaacb", "aacb", "acb"

Example 3

Input
s = "abc"
Output
1

Constraints

  • 3 <= s.length <= 5 * 10^4
  • s only consists of a, b or c characters.

Read it once before you touch anything

Want some pressure? Start the timer. In an interview you get about two minutes to understand the problem.

  1. 1Read the goalWhat goes in, what comes out
  2. 2Scan the examplesCheck one by hand
  3. 3Note the constraintsThey set your Big O
Say what it asks in one sentence
Say it out loud first, then compare with ours.