Given a string s&nbsp;consisting only of characters a, b and c.

Return the number of substrings containing at least&nbsp;one occurrence of all these characters a, b and c.

&nbsp;
Example 1:

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


Example 2:

Input: s = "aaacb"
Output: 3
Explanation: The substrings containing&nbsp;at least&nbsp;one occurrence of the characters&nbsp;a,&nbsp;b&nbsp;and&nbsp;c are "aaacb", "aacb" and "acb". 


Example 3:

Input: s = "abc"
Output: 1


&nbsp;
Constraints:


	3 &lt;= s.length &lt;= 5 x 10^4
	s&nbsp;only consists of&nbsp;a, b or c&nbsp;characters.


class Solution:
    def numberOfSubstrings(self, s: str) -> int:
        start = 0
        end = 0
        counter = 0
        store = {'a' : 0, 'b' : 0, 'c' : 0}

        for end in range(len(s)):
            store[s[end]] += 1

            while store['a'] > 0 and store['b'] > 0 and store['c'] > 0:
                counter += (len(s) - end)
                store[s[start]] -= 1
                start += 1

        return counter
class Solution {
    public int numberOfSubstrings(String s) {
        int a = 0, b = 0, c = 0, count = 0;
        for (int i = 0; i < s.length(); i++) {
            switch (s.charAt(i)) {
                case 'a': ++a; break;
                case 'b': ++b; break;
                case 'c': ++c; break;
            }
            if (a > 0 && b > 0 && c > 0) {
                while (a > 0 && b > 0 && c > 0) {
                    char farLeft = s.charAt(i - a - b - c + 1);
                    switch (farLeft) {
                        case 'a': {
                            --a;
                            count += doCount(s, i);
                            break;
                        }
                        case 'b': {
                            --b;
                            count += doCount(s, i);
                            break;
                        }
                        case 'c': {
                            --c;
                            count += doCount(s, i);
                            break;
                        }
                    }
                }
            }
        }
        return count;
    }

    private int doCount(String s, int i) {
        int count = 0;
        int n = s.length() - i;
        if (n > 0) {
            count += n;
        }
        return count;
    }
}
class Solution {
public:
    int numberOfSubstrings(string s) {
        int i=0,j=0;
        int n = s.size();
        map<char,int> mp;
        int count=0;
        while(j<n){
            mp[s[j]]++;
            if(mp.size()<3){
                j++;
            }
            else{
               while(mp.size()==3){
                   count+=(n-j);
                   mp[s[i]]--;
                   if(mp[s[i]]==0) mp.erase(s[i]);
                   i++;
                }
                j++;
            }
        }
        return count;
    }
};
/**
 * @param {string} s
 * @return {number}
 */
var numberOfSubstrings = function(s) {
    let ans = 0;
    let map = {};
    for(let i = 0, l = 0; i < s.length; i++) {
        const c = s[i];
        map[c] = (map[c] || 0) + 1;

        while(Object.keys(map).length == 3) {
            ans += s.length - i;
            map[s[l]]--;
            if(map[s[l]] == 0) {
                delete map[s[l]];
            }
            l++;
        }
    }
    return ans;
};
Number of Substrings Containing All Three Characters