Number of Substrings Containing All Three Characters

Medium Topics Company Tags

Count the contiguous substrings of s that contain all three letters: a, b, and c. A letter may appear more than once. Substrings at different positions count separately, even if their text is identical.

Example 1:

Input: s = "abca"

Output: 3

Explanation: The valid substrings are "abc", "bca", and "abca". Each contains at least one of a, b, and c.

Example 2:

Input: s = "aaab"

Output: 0

Explanation: The string contains no "c", so none of its substrings can contain all three letters.

Example 3:

Input: s = "cabca"

Output: 6

Explanation: There are three valid substrings starting at index 0 ("cab", "cabc", "cabca"), two starting at index 1 ("abc", "abca"), and one at index 2 ("bca").

Constraints:

  • 3 <= s.length <= 50000
  • Each character is a, b, or c.


Topics

Company Tags

Please upgrade to NeetCode Pro to view company tags.