Number of Substrings Containing All Three Characters

Given a string s consisting only of characters a, b, and c.

Return the number of substrings containing at least one occurrence of all three characters a, b, and c.

Example 1
Inputs = "abcabc"
Output10
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
Inputs = "aaacb"
Output3
The substrings containing at least one occurrence of the characters a, b and c are "aaacb", "aacb" and "acb".

Constraints

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

Asked at 6 companies

</>

Your Solution

(Ctrl/Cmd + Enter)

Switching Language

Loading template...

Loading...

Sign in to save your progress

AI code evaluation

Get a correctness verdict, missed edge cases, and complexity analysis of your solution.

Sign in to evaluate