Given a string s that consists only of the characters 'a', 'b', and 'c', return the number of substrings that contain at least one occurrence of each of the three characters.
Constraints:
s.length
s consists only of characters 'a', 'b', or 'c'.
The key idea is to use a sliding window over s to count substrings that contain at least one 'a', 'b', and 'c' without enumerating all substrings. The traversal starts with both pointers (left and right) at the beginning of the string, and the window is expanded one character at a time by moving right forward. As the window expands, keep track of whether it currently contains at least one 'a', at least one 'b', and at least one 'c'.
The moment the window contains all three characters, many substrings can be counted at once. That’s because if s[left..right] already has 'a', 'b', and 'c', then any substring that starts at the same left and ending at right or later (e.g. right + 1) will still have all three ...
Given a string s that consists only of the characters 'a', 'b', and 'c', return the number of substrings that contain at least one occurrence of each of the three characters.
Constraints:
s.length
s consists only of characters 'a', 'b', or 'c'.
The key idea is to use a sliding window over s to count substrings that contain at least one 'a', 'b', and 'c' without enumerating all substrings. The traversal starts with both pointers (left and right) at the beginning of the string, and the window is expanded one character at a time by moving right forward. As the window expands, keep track of whether it currently contains at least one 'a', at least one 'b', and at least one 'c'.
The moment the window contains all three characters, many substrings can be counted at once. That’s because if s[left..right] already has 'a', 'b', and 'c', then any substring that starts at the same left and ending at right or later (e.g. right + 1) will still have all three ...