[C++] Neatly Explained Sliding-Window Solution
The primary aim of the question is to find all substrings which satisfy the given conditions n(A),n(B),n(C) > 0. We can solve the problem by use of sliding wind
The primary aim of the question is to find all substrings which satisfy the given conditions n(A),n(B),n(C) > 0. We can solve the problem by use of sliding window in the following way\n1. Keep Expanding the sliding windows rightwards until the above condition is satisfied. \n2. Now, the sliding window contains a substring which satisifies the required conditions. However, when counting the total no.of substrings we need to consider all those other strings which could have the sliding widow substring as their prefix.\nFor eg: If we consider the string "aabccc" and the sliding window "abc" we also need to count strings "abcc", "abccc" which have their prefix as "abc"\n3. Once all substrings are counted, compress the sliding window leftwards and continue this process.\n\n```\nclass Solution {\npublic:\n    // Increment Frequency according to character\n    void HelperIncrement(char curr, int &nA, int &nB, int &nC) {\n        if(curr == \'a\') nA++;\n        if(curr == \'b\') nB++;\n        if(curr == \'c\') nC++;\n    }\n    \n    // Decrement Frequency according to character \n    void HelperDecrement(char curr, int &nA, int &nB, int &nC) {\n        if(curr == \'a\') nA--;\n        if(curr == \'b\') nB--;\n        if(curr == \'c\') nC--;  \n    }\n    \n    int numberOfSubstrings(string s) {\n        \n        // Initialize Window to currently hold the first character and update its \n        // corresponding frequency\n       int startptr = 0, endptr = 0;\n       int nA = 0, nB = 0, nC = 0;\n       HelperIncrement(s[0],nA,nB,nC);\n       int total_currsum = 0;\n        \n       while(startptr <= endptr) {\n           if(nA && nB && nC) {\n               // If Current window holds satisfies the necessary conditions i.e. nA,Nb,nC >= 1\n               // We add the total no.of strings having this window as its prefix \n               // Further we increment startptr(decrease window) and decrement frequency accordingly\n               total_currsum += s.size() - endptr;\n               HelperDecrement(s[startptr++],nA,nB,nC);\n           }\n           else\n           {\n               // This means that the current window does not contain at least 1 a,b & c\n               // Thus we increase the size of sliding window rightwards\n             endptr++;\n             if(endptr == s.size()) break;\n             HelperIncrement(s[endptr],nA,nB,nC);\n           } \n       } \n        return total_currsum;\n    }\n};\n```
[]