2 Approaches | Intuitive | TC :: O(n) | SC :: O(1) | Beats 100% | C++
IntuitionThe problem requires us to find the number of substrings jisme at least one occurrence of 'a', 'b', and 'c' ho. The idea yeh hai to use the sliding win
## **Intuition**  
The problem requires us to **find** the number of substrings **jisme** at least one occurrence of **'a'**, **'b'**, and **'c'** ho. The **idea yeh hai** to use the **sliding window** technique **jo ki** efficiently **help karega** in solving the problem.  

---

## **Approach 1: Sliding Window**  
1. Use two **pointers** **low** and **high** to define a window **jo ki** slide karega over the string.  
2. Expand the **high** pointer and **track karein** occurrences of **'a'**, **'b'**, and **'c'** inside the window.  
3. **Jaise hi** all three characters present **ho jayein** in the window, increment the count of valid substrings.  
4. Shrink the **low** pointer **jab tak** substring no longer contains all three characters.  
5. Every time we **find** a valid substring, we add `(length of string - high)` **kyunki** saare substrings starting from `low` to `high` till the end of the string bhi valid honge.  
6. Continue **jab tak** `high` reaches the end of the string.

### **Code**

```cpp
/*
By   :: savetrees
Used :: Sliding Window
*/
class Solution {
public:
    int numberOfSubstrings(string s) {
        int low = 0, high = 0, a = 0, b = 0, c = 0, cnt = 0;
        while (high < s.length()) {
            if (s[high] == 'a') a++;
            else if (s[high] == 'b') b++;
            else c++;
            
            while (a >= 1 && b >= 1 && c >= 1) {
                cnt += s.length() - high;
                if (s[low] == 'a') a--;
                else if (s[low] == 'b') b--;
                else c--;
                low++;
            }
            high++;
        }
        return cnt;
    }
};
```
### **Time Complexity:** O(n)  
- Each character **at most** twice process hota hai (once when expanding `high`, once when shrinking `low`).  
- **Is wajah se**, the complexity **O(n)** hai.  

### **Space Complexity:** O(1)  
- Only a few **integer variables use ho rahi hai**, **isliye** **O(1)** auxiliary space hai.  

---

## **Approach 2: Optimized Sliding Window (Bit More Efficient)**  
Instead of maintaining **alag-alag** variables for 'a', 'b', and 'c', we use an **array count[3]** indexed by `s[i] - 'a'`. **Isse code cleaner ho jata hai** and slightly **zyada efficient** bhi ho jata hai.

### **Code**
```cpp
/*
By   :: savetrees
Used :: Sliding Window ( Bit More Efficient)
*/
class Solution {
public:
    int numberOfSubstrings(string s) {
        int count[3] = {0, 0, 0}, low = 0, res = 0;
        for (int high = 0; high < s.length(); high++) {
            count[s[high] - 'a']++;
            while (count[0] && count[1] && count[2]) {
                res += s.length() - high;
                count[s[low++] - 'a']--;
            }
        }
        return res;
    }
};
```
### **Time Complexity:** O(n)  
- **Same reasoning** as the previous approach, **O(n)** overall.  

### **Space Complexity:** O(1)  
- The array `count[3]` is **constant-size storage**, **toh space bhi O(1) hai**.  

---

## **Example Walkthrough**  
### **Input:** `"abcabc"`  
### **Step-by-step execution:**  

| Window (`low` to `high`) | `count[a]` | `count[b]` | `count[c]` | Substrings Count |
|-----------------|----------|----------|----------|--------------|
| `"a"` | 1 | 0 | 0 | 0 |
| `"ab"` | 1 | 1 | 0 | 0 |
| `"abc"` | 1 | 1 | 1 | `6-2=4` |
| `"bca"` | 1 | 1 | 1 | `6-3=3` |
| `"cab"` | 1 | 1 | 1 | `6-4=2` |
| `"abc"` | 1 | 1 | 1 | `6-5=1` |

### **Final Output:** `10`  

Thus, **dono approaches efficiently count karte hain** all valid substrings while maintaining **O(n) time complexity** and **O(1) space complexity**.
['C++']