🔥Beats 100% | Easy and Visual Explaination | Beginner Level | Sliding Window Approach
:) Problem StatementWe are given a string s consisting only of characters 'a', 'b', and 'c'.
We need to find the number of substrings that contain at least one

# :) Problem Statement
We are given a string `s` consisting only of characters `'a'`, `'b'`, and `'c'`.
We need to find the number of substrings that contain at least one occurrence of all three characters.

---

# :) Understanding the Problem with Example📝
Let's take `s = "abcabc"` and find the valid substrings :

![temp1.png](https://assets.leetcode.com/users/images/f1be8623-0d69-4491-8f7b-d9951225df08_1741677749.9404738.png)

Total **10** valid substrings.

---

# :) Efficient Sliding Window Approach 💡
Instead of generating all substrings (which would be slow), we use a smart approach to count substrings dynamically.
### Key Idea :
1. Keep track of the latest index where `a`, `b`, and `c` appeared.
1. If we find a valid substring ending at index `i`, then all previous substrings including it are also valid.
1. Add the minimum of the latest occurrences of `a`, `b`, and `c` to the count.

---
# :) Example Step by Step
## Example 1 : s = "abcabc"

![temp2.png](https://assets.leetcode.com/users/images/7769495b-7e22-411b-9677-946983d5c95d_1741678044.834172.png)

✅ Answer : **10**

## Example 2 : s = "aaacb"

![temp3.png](https://assets.leetcode.com/users/images/529abbf5-68a0-43de-977e-642bb1f44ed2_1741678185.4706802.png)

✅ Answer : **3**

## Example 3 : s = "abc"

![temp4.png](https://assets.leetcode.com/users/images/302cd3a2-74bc-48d1-8bd6-dc138a3fb5fa_1741678223.8421338.png)

✅ Answer : **1**


---
![upvote.jpg](https://assets.leetcode.com/users/images/c0de7e20-2bae-4a23-ae06-37cf9002f6cd_1741677567.7852337.jpeg)

---

# :) Complexity
- Time complexity : O(n) We only traverse the string once and update indices efficiently.
<!-- Add your time complexity here, e.g. $$O(n)$$ -->

- Space complexity : O(1) We only use a few integer variables.
<!-- Add your space complexity here, e.g. $$O(n)$$ -->

---

# :) Bro'Code

```javascript []
var numberOfSubstrings = function (s) {
    let a = -1
    let b = -1
    let c = -1
    let count = 0
    for(let i=0;i<s.length;i++){
        if(s[i]=="a"){
            a = i
        }else if(s[i] == "b"){
            b= i
        }else if(s[i] == "c"){
            c = i
        }

        count += Math.min(a,b,c)+1
    }

    return count
};
```
```typescript []
function numberOfSubstrings(s: string): number {
    let a = -1, b = -1, c = -1, count = 0;
    
    for (let i = 0; i < s.length; i++) {
        if (s[i] === "a") a = i;
        else if (s[i] === "b") b = i;
        else if (s[i] === "c") c = i;

        count += Math.min(a, b, c) + 1;
    }

    return count;
}
```
```C []
int numberOfSubstrings(char* s) {
    int a = -1, b = -1, c = -1, count = 0;
    int length = strlen(s);

    for (int i = 0; i < length; i++) {
        if (s[i] == 'a') a = i;
        else if (s[i] == 'b') b = i;
        else if (s[i] == 'c') c = i;

        count += (a >= 0 && b >= 0 && c >= 0) ? (1 + (a < b ? (a < c ? a : c) : (b < c ? b : c))) : 0;
    }

    return count;
}
```
```C++ []
class Solution {
public:
    int numberOfSubstrings(string s) {
        int a = -1, b = -1, c = -1, count = 0;
        
        for (int i = 0; i < s.size(); i++) {
            if (s[i] == 'a') a = i;
            else if (s[i] == 'b') b = i;
            else if (s[i] == 'c') c = i;
            
            count += min({a, b, c}) + 1;
        }
        
        return count;
    }
};
```
```C# []
public class Solution {
    public int NumberOfSubstrings(string s) {
        int a = -1, b = -1, c = -1, count = 0;

        for (int i = 0; i < s.Length; i++) {
            if (s[i] == 'a') a = i;
            else if (s[i] == 'b') b = i;
            else if (s[i] == 'c') c = i;

            count += Math.Min(a, Math.Min(b, c)) + 1;
        }

        return count;
    }
}
```
```Java []
class Solution {
    public int numberOfSubstrings(String s) {
        int a = -1, b = -1, c = -1, count = 0;
        
        for (int i = 0; i < s.length(); i++) {
            if (s.charAt(i) == 'a') a = i;
            else if (s.charAt(i) == 'b') b = i;
            else if (s.charAt(i) == 'c') c = i;

            count += Math.min(a, Math.min(b, c)) + 1;
        }
        
        return count;
    }
}
```
```python []
class Solution(object):
    def numberOfSubstrings(self, s):
        """
        :type s: str
        :rtype: int
        """
        a, b, c = -1, -1, -1
        count = 0

        for i in range(len(s)):
            if s[i] == 'a':
                a = i
            elif s[i] == 'b':
                b = i
            elif s[i] == 'c':
                c = i

            count += min(a, b, c) + 1
        
        return count
```
```Go []
func numberOfSubstrings(s string) int {
    a, b, c := -1, -1, -1
    count := 0

    for i, ch := range s {
        if ch == 'a' {
            a = i
        } else if ch == 'b' {
            b = i
        } else if ch == 'c' {
            c = i
        }

        count += min(a, min(b, c)) + 1
    }

    return count
}

func min(x, y int) int {
    if x < y {
        return x
    }
    return y
}
```
```ruby []
def number_of_substrings(s)
    a = -1
    b = -1
    c = -1
    count = 0

    s.chars.each_with_index do |char, i|
        if char == 'a'
            a = i
        elsif char == 'b'
            b = i
        elsif char == 'c'
            c = i
        end

        count += [a, b, c].min + 1 if [a, b, c].min >= 0
    end

    count
end
```
['C', 'Sliding Window', 'Python', 'C++', 'Java', 'Go', 'TypeScript', 'Ruby', 'JavaScript', 'C#']