Two Pointer 🔥| Beginner Friendly Explanation🚀 | Java | Cpp | Python | JavaScript
IntuitionWe need to count the number of substrings that contain all three characters 'a', 'b', and 'c'. Using a two-pointer sliding window approach, we can effi
# Intuition
We need to count the number of substrings that contain all three characters 'a', 'b', and 'c'. Using a **two-pointer sliding window approach**, we can efficiently track when all three characters are present and count the valid substrings.

# Approach
1. Use a **sliding window (two-pointer)** approach with `i` and `j`.
2. Maintain an array `contains[3]` to track counts of 'a', 'b', and 'c'.
3. Expand the right pointer `j` and update the `contains` array.
4. If all three characters are found, move the left pointer `i` to count valid substrings.
5. The number of valid substrings is increased by `(size - j)` each time a valid substring is found.
6. Continue until the entire string is processed.

# Complexity
- Time complexity: $$O(n)$$
- Space complexity: $$O(1)$$

# Code

```java []
class Solution {
    public int numberOfSubstrings(String s) {
        int size = s.length();
        if(size < 3) return 0;

        int[] contains = new int[3];
        int i = 0, j = 0, ans = 0;

        while(j < size){
            contains[s.charAt(j) - 'a']++;

            while(contains[0] > 0 && contains[1] > 0 && contains[2] > 0){
                ans += size - j;
                contains[s.charAt(i) - 'a']--;
                i++;
            }
            j++;
        }
        return ans;
    }
}
```
```python []
class Solution:
    def numberOfSubstrings(self, s: str) -> int:
        size = len(s)
        if size < 3:
            return 0

        contains = [0] * 3
        i = j = ans = 0

        while j < size:
            contains[ord(s[j]) - ord('a')] += 1

            while all(contains):
                ans += size - j
                contains[ord(s[i]) - ord('a')] -= 1
                i += 1
            
            j += 1

        return ans
```
``` cpp []
#include <iostream>
#include <vector>
using namespace std;

class Solution {
public:
    int numberOfSubstrings(string s) {
        int size = s.length();
        if (size < 3) return 0;

        vector<int> contains(3, 0);
        int i = 0, j = 0, ans = 0;

        while (j < size) {
            contains[s[j] - 'a']++;

            while (contains[0] > 0 && contains[1] > 0 && contains[2] > 0) {
                ans += size - j;
                contains[s[i] - 'a']--;
                i++;
            }
            j++;
        }

        return ans;
    }
};
```
```JavaScript []
var numberOfSubstrings = function(s) {
    let size = s.length;
    if (size < 3) return 0;

    let contains = [0, 0, 0];
    let i = 0, j = 0, ans = 0;

    while (j < size) {
        contains[s.charCodeAt(j) - 'a'.charCodeAt(0)]++;

        while (contains[0] > 0 && contains[1] > 0 && contains[2] > 0) {
            ans += size - j;
            contains[s.charCodeAt(i) - 'a'.charCodeAt(0)]--;
            i++;
        }
        j++;
    }

    return ans;
};
```
**Please Upvote⬆️**
![Screenshot 2025-02-26 230220.png](https://assets.leetcode.com/users/images/d0296d22-d7bf-404b-9d84-c4c226228f3b_1742065945.6334765.png)


['Array', 'Math', 'Two Pointers', 'Python', 'C++', 'Java', 'JavaScript']