Easiest solution | Best Explanation
IntuitionThe problem requires us to find substrings containing at least one occurrence of each character ('a', 'b', and 'c'). A sliding window approach is ideal
### Intuition
The problem requires us to find substrings containing at least one occurrence of each character ('a', 'b', and 'c'). A sliding window approach is ideal because it efficiently tracks a range of substrings and dynamically verifies their validity.

### Approach
1. Use two pointers (`i` and `j`) to represent the bounds of a sliding window.
2. Maintain counters (`ac`, `bc`, and `cc`) to track the occurrences of 'a', 'b', and 'c' in the current window.
3. Expand the window by incrementing `j` and updating the counters for the character at `j`.
4. Whenever all three counters are greater than 0, calculate the number of valid substrings ending at `j`. This is equal to `s.length() - j`. Then, shrink the window by incrementing `i` and decrementing the corresponding counter for the character at `i`.
5. Repeat until `j` reaches the end of the string.

### Complexity
- **Time complexity:**  
  $$O(n)$$  
  Both pointers, `i` and `j`, traverse the string at most once.

- **Space complexity:**  
  $$O(1)$$  
  We use a constant amount of extra space for counters and pointers.

# Code
```java []
class Solution {
    public int numberOfSubstrings(String s) {
        int i = 0;
        int j = 0;
        int ac = 0, bc = 0, cc = 0; // Counters for 'a', 'b', and 'c'
        int ans = 0; // To store the final result

        while (j < s.length()) {
            // Expand the window
            if (s.charAt(j) == 'a') ac++;
            if (s.charAt(j) == 'b') bc++;
            if (s.charAt(j) == 'c') cc++;

            // Shrink the window if it contains all characters
            while (ac > 0 && bc > 0 && cc > 0) {
                ans += s.length() - j; // Count substrings ending at j
                if (s.charAt(i) == 'a') ac--;
                if (s.charAt(i) == 'b') bc--;
                if (s.charAt(i) == 'c') cc--;
                i++; // Shrink the window
            }

            j++; // Move the right pointer
        }

        return ans;
    }
}

```
['Java']