Number of Substrings Containing All Three Characters

Given a string `s` consisting only of the characters `a`, `b` and `c`, return the number of substrings containing at least one occurrence of each of these three characters.

30:00

Number of Substrings Containing All Three Characters
medium
Free written solution
Topics
Companies

Given a string s consisting only of the characters a, b and c, return the number of substrings containing at least one occurrence of each of these three characters.

Example 1:
Input: {"s":"abcabc"}
Output: 10
Constraints:
  • 3≤s.length≤5×1043 \leq \text{s.length} \leq 5 \times 10^4

  • ss only consists of the characters a, b or c.

Test Cases
Input (Stdin)
Expected Output (Stdout)
Execution Result

Run code to see results.

Input
arr ={"s":"abcabc"}

Count substrings by where they end. Remember the last index of a, b and c; -1 means not seen yet.

s

a
b
c
a
b
c

0

1

2

3

4

5

last seen

a: not seen

b: not seen

c: not seen

last a

-1

last b

-1

last c

-1

count

0

Variables
VariableValue
last[-1,-1,-1]
count0
DepthFunction Call
Stack empty
0/13