Skip to content

Number of Substrings Containing All Three Characters

MediumSliding Window
Decode it first. Clues, constraints, pattern and plan before any code.

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.

s = "abcabc"   →  10
s = "aaacb"    →  3    ("aaacb", "aacb", "acb")
s = "abc"      →  1

Constraints

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