Sliding window| Optimal Approach| Beginner Friendly
\n\n# Code\ncpp []\nclass Solution {\npublic:\n    int numberOfSubstrings(string s, int k) {\n        //abcde k=1\n        int i,j=0;\n        int n=s.size();\n
\n\n# Code\n```cpp []\nclass Solution {\npublic:\n    int numberOfSubstrings(string s, int k) {\n        //abcde k=1\n        int i,j=0;\n        int n=s.size();\n        \n        map<char,int>mp;\n        int cnt=0;\n        // just as soon as a frequency  gets to k you can count all substring by simple math because  all substrings ending at any index from here on will have this freq ==k\n        while(j<n){\n           \n        mp[s[j]]++;\n            while(mp[s[j]]==k){\n                cnt+=(n-j);\n               \n                 \n                   \n              mp[s[i++]]--;  \n                }\n            j++;\n            \n            }\n        return cnt;\n        \n    }\n};\n```
['Hash Table', 'String', 'Sliding Window', 'C++']