Beats 100% | Java, C++,C, Python3, C#, JS, TS | Optimised Approach.
🚀 Beats 100% | Sliding Window | Optimized Approach🔼 Please Upvote🔼 Please Upvote🔼 Please Upvote🔼 Please Upvote💡 If this helped, don’t forget to upvote! 🚀🔥💡 Prob
https://youtu.be/iRBpwsp5xWQ

![image.png](https://assets.leetcode.com/users/images/134c89ed-8595-4aed-833e-e9c5e910055c_1741668824.632005.png)


---

# 🚀 **Beats 100% | Sliding Window | Optimized Approach**  

---

# **🔼 Please Upvote**  
# **🔼 Please Upvote**  
# **🔼 Please Upvote**  
# **🔼 Please Upvote**  

💡 **If this helped, don’t forget to upvote! 🚀🔥**  

---

## **💡 Problem Breakdown**  
We are given a string **s** containing only lowercase English letters.  
We need to count the number of substrings that contain **at least one occurrence** of each character: `'a'`, `'b'`, and `'c'`.  

---

## **🔍 Approach Explanation**  
🔹 **Sliding Window Technique**: Expand the window from the right and adjust the left pointer accordingly.  
🔹 **Tracking Last Occurrences**: Use an array to store the last seen index of `'a'`, `'b'`, and `'c'`.  
🔹 **Valid Substring Counting**: The minimum index among the three indicates how many valid substrings can be formed ending at the current index.  

---

## **⏳ Complexity Analysis**  
- **Time Complexity:** `O(N)`, where `N` is the length of `s`.  
- **Space Complexity:** `O(1)`, since only a fixed array is used.  

---

## **📝 Code Implementation**  

### **🔵 Java Solution**  
```java
class Solution {
    public int numberOfSubstrings(String s) {
        char[] ch = s.toCharArray();
        int[] abc = new int[3];
        for(int i = 0; i < abc.length; i++){
            abc[i] = -1;
        }
        int count = 0, right = 0;
        while(right < ch.length){
            abc[ch[right] - 'a'] = right;
            int minIndex = Integer.MAX_VALUE;
            for(int i = 0; i < 3; i++){
                minIndex = Math.min(minIndex, abc[i]);
            }
            count += (minIndex + 1);
            right++;
        }
        return count;
    }
}
```  

---

### **🟢 C++ Solution**  
```cpp
class Solution {
public:
    int numberOfSubstrings(string s) {
        int abc[3] = {-1, -1, -1};
        int count = 0, right = 0;
        while (right < s.length()) {
            abc[s[right] - 'a'] = right;
            int minIndex = INT_MAX;
            for (int i = 0; i < 3; i++) {
                minIndex = min(minIndex, abc[i]);
            }
            count += (minIndex + 1);
            right++;
        }
        return count;
    }
};

```  

---

### **🟣 Python3 Solution**  
```python
class Solution:
    def numberOfSubstrings(self, s: str) -> int:
        abc = [-1, -1, -1]
        count, right = 0, 0
        while right < len(s):
            abc[ord(s[right]) - ord('a')] = right
            minIndex = min(abc)
            count += (minIndex + 1)
            right += 1
        return count

```  

---

### **🔴 C Solution**  
```c
#include <string.h>
#include <limits.h>

int numberOfSubstrings(char* s) {
    int abc[3] = {-1, -1, -1};
    int count = 0, right = 0;
    while (s[right] != '\0') {
        abc[s[right] - 'a'] = right;
        int minIndex = INT_MAX;
        for (int i = 0; i < 3; i++) {
            if (abc[i] < minIndex) {
                minIndex = abc[i];
            }
        }
        count += (minIndex + 1);
        right++;
    }
    return count;
}

```  

---

### **🟠 C# Solution**  
```csharp
public class Solution {
    public int NumberOfSubstrings(string s) {
        char[] ch = s.ToCharArray();
        int[] abc = new int[3];
        for (int i = 0; i < abc.Length; i++) {
            abc[i] = -1;
        }
        int count = 0, right = 0;
        while (right < ch.Length) {
            abc[ch[right] - 'a'] = right;
            int minIndex = int.MaxValue;
            for (int i = 0; i < 3; i++) {
                minIndex = Math.Min(minIndex, abc[i]);
            }
            count += (minIndex + 1);
            right++;
        }
        return count;
    }
}

```  

---

### **🟡 JavaScript Solution**  
```javascript
var numberOfSubstrings = function(s) {
    let ch = s.split('');
    let abc = [-1, -1, -1];
    for (let i = 0; i < 3; i++) {
        abc[i] = -1;
    }
    let count = 0, right = 0;
    while (right < ch.length) {
        abc[ch[right].charCodeAt(0) - 'a'.charCodeAt(0)] = right;
        let minIndex = Math.min(...abc);
        count += (minIndex + 1);
        right++;
    }
    return count;
};

```  

---

### **🟣 TypeScript Solution**  
```typescript
function numberOfSubstrings(s: string): number {
    let ch: string[] = s.split('');
    let abc: number[] = [-1, -1, -1];
    for (let i = 0; i < 3; i++) {
        abc[i] = -1;
    }
    let count = 0, right = 0;
    while (right < ch.length) {
        abc[ch[right].charCodeAt(0) - 'a'.charCodeAt(0)] = right;
        let minIndex = Math.min(...abc);
        count += (minIndex + 1);
        right++;
    }
    return count;
}

```

---

# **🔼 Please Upvote**  
💡 **Let me know if you want further optimizations! 🚀🔥**  

---

🚀 **If you found this useful, please give it an upvote!** 🔥
['String', 'C', 'Sliding Window', 'C++', 'Java', 'TypeScript', 'Python3', 'JavaScript', 'C#']