> For the complete documentation index, see [llms.txt](https://coding-9.gitbook.io/untitled/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://coding-9.gitbook.io/untitled/sliding-window/medium/1358.-number-of-substrings-containing-all-three-characters.md).

# 1358. Number of Substrings Containing All Three Characters

## Problem Statement

<br>

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/>

### 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

###
