#1358

Number of Substrings Containing All Three Characters

pupil · 510 · lc medium +27 · verified · 73.5% accepted · 4,408 likes · top 84%

Description

Given a string s composed entirely of 'a', 'b', and 'c', count the number of substrings that contain at least one occurrence of each of the three characters.

Example 1:

Input: s = "abcabc"
Output: 10
Explanation: The substrings containing at least one occurrence of the characters a, b and c are "abc", "abca", "abcab", "abcabc", "bca", "bcab", "bcabc", "cab", "cabc" and "abc" (again).

Example 2:

Input: s = "aaacb"
Output: 3
Explanation: The substrings containing at least one occurrence of the characters a, b and c are "aaacb", "aacb" and "acb".

Example 3:

Input: s = "abc"
Output: 1

Code

1
2
3