# 1358. Number of Substrings Containing All Three Characters

## Problem Statement

\


Given a string `s` consisting only of characters _a_, _b_ and _c_.

Return the number of substrings containing at least one occurrence of all these characters _a_, _b_ and _c_.

&#x20;

**Example 1:**

<pre><code><strong>Input: s = "abcabc"
</strong><strong>Output: 10
</strong><strong>Explanation: The substrings containing at least one occurrence of the characters a, b and c are "abc", "abca", "abcab", "abcabc", "bca", "bcab", "bcabc", "cab", "cabc" and "abc" (again). 
</strong></code></pre>

**Example 2:**

<pre><code><strong>Input: s = "aaacb"
</strong><strong>Output: 3
</strong><strong>Explanation: The substrings containing at least one occurrence of the characters a, b and c are "aaacb", "aacb" and "acb". 
</strong></code></pre>

**Example 3:**

<pre><code><strong>Input: s = "abc"
</strong><strong>Output: 1
</strong></code></pre>

&#x20;

**Constraints:**

* `3 <= s.length <= 5 x 10^4`
* `s` only consists of _a_, _b_ or _c_ characters.

## Intuition

```
Approach:

As an when we get all occurences of a, b and c
We take all the sub-arrays  from that point till end
eg-
abc|abc
At index 2 we get all
So we take n-j
6-2=4 We get 4 sub arrays : abc, abca, abcab, abcabc
Like that
```

### Links

[https://leetcode.com/problems/number-of-substrings-containing-all-three-characters/description/](https://leetcode.com/problems/number-of-substrings-containing-all-three-characters/description/)

### Video Links



### Approach 1:

```
Sliding Window
```

{% code title="C++" lineNumbers="true" %}
```cpp
class Solution {
public:
    int numberOfSubstrings(string s) {
        unordered_map<char,int> mp;
        int ans=0, low=0, n=s.size();

        for(int high=0; high<s.size(); high++){
            mp[s[high]]++;

            while(mp['a'] and mp['b'] and mp['c']){
                ans += n-high;
                mp[s[low]]--;
                low++;
            }
        }

        return ans;
    }
};
```
{% endcode %}

### Approach 2:

```
```

{% code title="C++" lineNumbers="true" %}
```cpp
```
{% endcode %}

### Approach 3:

```
```

{% code title="C++" lineNumbers="true" %}
```cpp
```
{% endcode %}

### Approach 4:

```
```

{% code title="C++" lineNumbers="true" %}
```cpp
```
{% endcode %}

### Similar Problems

###

