🔥Beats 90% 🎯| ✨Easy Sliding Window Approach | C++ | Java
IntuitionThe problem requires counting the number of substrings that contain at least one occurrence of the characters a, b, and c. we use the Sliding Window te
# Intuition
The problem requires counting the number of substrings that contain at least one occurrence of the characters `a`, `b`, and `c`. we use the **Sliding Window** technique to efficiently count valid substrings.

****

# Approach
1. **Sliding Window:**
    - Maintain two pointers `left` and `right`. Expan the window by moving `right` pointer.
    - Use a **Hashmap** or **Count Array** to count the frequency of `a`, `b` and `c`.
    - If the window contains all three characters, it is valid.

2. **Counting Substrings:**
    - If the window is valid then all the substring that start winthin the window and extend to the right are also valid.
    - Increment the count by `s.length - right`

3. **Decrementing the Window Size:**
    - Move the `left` pointer until the windoe is invalid.

****

# Complexity
- **Time complexity:**
    - **O(N)** -> Each character is processed at most twice.
- **Space complexity:**
    - **O(1)** -> HashMap is fixed size of 3.

****

# Code
```cpp []
class Solution {
public:
    int numberOfSubstrings(string s) {
        int n = s.size(), left = 0, res = 0;
        vector<int> freq(3, 0);

        for(int right = 0; right < n; right++){
            freq[s[right] - 'a']++;

            while((freq[0] > 0) && (freq[1] > 0) && (freq[2] > 0)){
                res += n - right;
                freq[s[left] - 'a']--;
                left++;
            }
        }

        return res;
    }
};
```
```java []
class Solution {
    public int numberOfSubstrings(String s) {
        int n = s.length(), left = 0, res = 0;
        int[] freq = new int[3];

        for(int right = 0; right < n; right++){
            int ch1 = s.charAt(right) - 'a';
            freq[ch1]++;

            while(freq[0] > 0 && freq[1] > 0 && freq[2] > 0){
                res += (n - right);
                int ch2 = s.charAt(left) - 'a';
                freq[ch2]--;
                left++;
            }
        }

        return res;
    }
}
```


['Hash Table', 'String', 'Sliding Window', 'C++', 'Java']