✅ C++ || Beginner [Explained] || Easy To Understand || Sliding Window || O(N) time || O(1) space
Approach\n\nThis is a very good hashmap problem\nProblem description is quite clear : we have to count all those substring of a given string s which have atleas
# ***Approach***\n```\nThis is a very good hashmap problem\nProblem description is quite clear : we have to count all those substring of a given string s which have atleast single[\'a\' and \'b\' and \'c\']\n\nNow the problem we face here is that finding all substrings of a string actually takes O(N*2) time , but we don\'t need to generate all substrings , Yes in problem we are just required to return number of substrings fulfilling given condtion!!!\n    \nOk but how can we find number of substrings without actually finding all substrings!!!\n    \nWe will use sliding window technique to solve this problem in O(N) time + we use frequency map to maintain counts of [\'a\' , \'b\' and \'c\']\n\nNOTE : string s will only contain [a,b and c] as characters , so size of frequency map will always be 3 , so Space is O(1)\n    \nOk how we can use sliding window to find number of substrings fulfilling condition?\n\nAns :\n        => let good window : window fulfilling condition\n\n        while(right != s.size())\n        {\n              We tries to form a good window of any size\n\n              Now once we have a good window , we add (1 + (end - right)) to count and then shrink this good window from front and form a new window [if new window is also a good window we repeat the same process]\n\n              Above line in code is like this!!!\n              while(window is good)\n              {\n                  count += (1 + (end - right));\n                  shrink window from front to form new window\n              }\n        }\n\n\n      [v. imp] why we are adding extra (end - right) to count?\n          Ans : because by adding this we are actually considering all those substrings which are bigger in size than current substring and also fully contains our current substring!!!\n```\n# ***Code***\n```\nclass Solution {\npublic:\n    int numberOfSubstrings(string s) {\n        // O(N) time , O(1) space [because hasmap size is always 3]\n        int left = 0 , right = 0 , end = s.size() - 1;\n        unordered_map<char,int> map;\n        \n        int count = 0;\n        \n        while(right != s.size())\n        {\n            map[s[right]] += 1; // adding new element to window\n            \n            while(map[\'a\'] and map[\'b\'] and map[\'c\']) // while(window is good)\n            {\n                count += 1 + (end - right);\n                \n\t\t\t\t// removing first element[means shrinking window from front to form new window]\n                map[s[left]] -= 1; \n                left++;\n            }\n            right++;\n        }\n        return count;\n    }\n};\n```\n# ***If you liked the solution , Please Upvote :)***
['C', 'Sliding Window']