← DiffPush

Number of Substrings Containing All Three Characters

Standard Bar Sliding Window · Medium Problems O(n) · O(1)

The Warehouse Zone Coverage Audit

A warehouse's floor plan is encoded as a strip where each position is zone A, B, or C. Compliance requires every audit team to cover a contiguous stretch that touches all three zone types at least once. The planner counts how many distinct stretches qualify — teams differ by where they start and end, not by which zones they see.

Input: A string s consisting only of 'a', 'b', and 'c'.

Output: Return the number of substrings that contain at least one 'a', one 'b', and one 'c'.

Constraints

Examples

Example 1

Input: {"s":"abcabc"}
Output: 10
Every stretch ending at index 3 or later qualifies; the count follows each right edge's earliest legal start.

Example 2

Input: {"s":"aaacb"}
Output: 3
Only stretches that reach the final 'c' and also touch 'a' and 'b' qualify.

Solve this in your browser →

Also on LeetCode ↗