Sliding window||C, C++beats 100% Py3
Intuitionstandard sliding window
C++ & Python are done.2nd C++/C uses the last positions for a, b, c which needs only 1 single loop.ApproachThese problems which
# Intuition
<!-- Describe your first thoughts on how to solve this problem. -->
standard sliding window
C++ & Python are done.

2nd C++/C uses the last positions for a, b, c which needs only 1 single loop.
# Approach
<!-- Describe your approach to solving the problem. -->
These problems which are listed below can be solved by the following pattern:
```
for(int l,r=0; r<n; r++){
    do_something_by_adding(s[r]);
    while (check_condition(k)){
        do_something_by_removing(s[l]);
        l++;
    }
    update_the_answer();
}
```
For this question:
`update_the_answer()` : cnt+=l
`check_condition(k)`: letter==3
`do_something_by_removing(s[l])`:`if (--freq[s[l]-'a']==0) letter--;`
`do_something_by_adding(s[r])` is 
```
const int c=s[r]-'a';
if (++freq[c]==1) letter++;
```
The following questions can be solved by this pattern for sliding window:
[713. Subarray Product Less Than K](https://leetcode.com/problems/subarray-product-less-than-k/solutions/4930759/2c-sliding-windows30ms-beats-9992/)
[2302. Count Subarrays With Score Less Than K](https://leetcode.com/problems/count-subarrays-with-score-less-than-k/solutions/4932340/sliding-windows-like-leetcode-713-44ms-beats-100/)
[2958. Length of Longest Subarray With at Most K Frequency](https://leetcode.com/problems/length-of-longest-subarray-with-at-most-k-frequency/solutions/4935191/sliding-window-hash-map128ms-beats-9978/)
[2962. Count Subarrays Where Max Element Appears at Least K Times
](https://leetcode.com/problems/count-subarrays-where-max-element-appears-at-least-k-times/solutions/4939830/sliding-window-vs-combinatorical-formula-59ms-beats-100/)[76. Minimum Window Substring](https://leetcode.com/problems/minimum-window-substring/solutions/4673600/sliding-windows-hash-tables-freq-array-0ms-beats-100/)<-hard
[Please turn on the English subtitles if necessary]
[https://youtu.be/pi1XpQU_Yxo?si=orqw7Ysq-fk_ID3y](https://youtu.be/pi1XpQU_Yxo?si=orqw7Ysq-fk_ID3y)
# Complexity
- Time complexity:
<!-- Add your time complexity here, e.g. $$O(n)$$ -->
$O(n)$
- Space complexity:
<!-- Add your space complexity here, e.g. $$O(n)$$ -->
$O(1)$
# Code||C++ 0ms beats 100%
```cpp []
class Solution {
public:
    static int numberOfSubstrings(string& s) {
        const int n=s.size();
        int cnt=0, letter=0, freq[3]={0};
        for(int l=0, r=0; r<n; r++){
            const int c=s[r]-'a';
            if (++freq[c]==1) letter++;
            while( letter==3 ){
                if (--freq[s[l]-'a']==0) letter--;
                l++;
            }
            cnt+=l;
        }
        return cnt;
    }
};


auto init = []() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);
    return 'c';
}();
```
```Python []
class Solution:
    def numberOfSubstrings(self, s: str) -> int:
        n, cnt, letter, l=len(s), 0, 0, 0
        freq=[0]*3
        for r, c in enumerate(s):
            freq[ord(c)-97]+=1
            if freq[ord(c)-97]==1: letter+=1
            while letter==3:
                freq[ord(s[l])-97]-=1
                if freq[ord(s[l])-97]==0: letter-=1
                l+=1
            cnt+=l
        return cnt
```
# 2nd C++ & C||C++ 0ms |C 3ms beats 100%|C  2nd ver 0ms

The trick  to track the last position for letters is also used in solving yesterday's problem [3306. Count of Substrings Containing Every Vowel and K Consonants II](https://leetcode.com/problems/count-of-substrings-containing-every-vowel-and-k-consonants-ii/solutions/6518863/count-lesseqword-k-lessword-k-1121ms-bea-hcto/)
```C [C 2nd ver]
#pragma GCC optimize("O3, unroll-loops")
inline int min(int x, int y){ return x<y?x:y; }
int numberOfSubstrings(char* s) {
    int cnt=0, letter=0, last[3]={-1, -1, -1};
    for (register int r=0; s[r]!='\0'; r++){
        const int c=s[r]-'a', c1=(c==2)?0:c+1, c2=(c==0)?2:c-1;
        last[c]=r;
        cnt+=1+min(last[c1], last[c2]);
    }
    return cnt;
}
```
```C++ []
class Solution {
public:
    static int numberOfSubstrings(string& s) {
        const int n=s.size();
        int cnt=0, letter=0, last[3]={-1, -1, -1};
        for(int r=0; r<n; r++){
            const int c=s[r]-'a';
            last[c]=r;
            cnt+=1+min(last[(c+1)%3], last[(c+2)%3]);
        }
        return cnt;
    }
};


auto init = []() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);
    return 'c';
}();
```
```C []
#pragma GCC optimize("O3, unroll-loops")
inline int min(int x, int y){ return x<y?x:y; }
int numberOfSubstrings(char* s) {
    int cnt=0, letter=0, last[3]={-1, -1, -1};
    for (register int r=0; s[r]!='\0'; r++){
        const int c=s[r]-'a';
        last[c]=r;
        cnt+=1+min(last[(c+1)%3], last[(c+2)%3]);
    }
    return cnt;
}
```
['C', 'Sliding Window', 'C++', 'Python3']