Number of Substrings Containing All Three Characters

Count substrings containing at least one a, b, and c — sliding window with `ans += left` trick.

Medium
Time O(n) Space O(1)
LeetCode
3 min read
sliding-window string

Problem#

Given a string s made of a, b, c, count the substrings that contain at least one of each.

Examples#

  • "abcabc"10
  • "aaacb"3

Constraints#

  • 3 <= s.length <= 5 * 10^4.

Approach#

Walk right, track counts of each character. While all three are present, shrink left. For each right, count = left valid starting positions (every prefix [0..left-1] extended to right is valid).

Solution#

Number of Substrings Containing All Three
class Solution {
public:
int numberOfSubstrings(string s) {
int cnt[3] = {0};
int left = 0, ans = 0;
for (int right = 0; right < (int)s.size(); ++right) {
++cnt[s[right] - 'a'];
while (cnt[0] && cnt[1] && cnt[2]) {
--cnt[s[left++] - 'a'];
}
ans += left;
}
return ans;
}
};

Editorial#

Once right is fixed and the window first becomes valid, any extension to the left (start ≤ left - 1) is also valid — so ans += left counts all of them simultaneously. After the inner loop, left is the smallest start that would be valid for the next right - 1, but for right itself we need the invalid start positions count, which is exactly left (positions 0..left-1 are valid as starts when paired with this right).

Wait — re-check the off-by-one: after the while loop, left is the first index where the window [left..right] is not yet covering all three. The valid starts are [0..left-1], count = left. Confirmed.

Complexity#

  • Time: O(n).
  • Space: O(1).

Concept revision#

Search ESC

Keyboard shortcuts

Shortcuts are disabled while typing in inputs.