Brute force (All Substrings) >> Optimal (Sliding Window)
\n# Brute Force\n\n\nclass Solution {\npublic:\n    \n    int subs(const string &s, int k) {\n        int n = s.length(); \n        int cnt = 0;\n        for (i
\n# Brute Force\n\n```\nclass Solution {\npublic:\n    \n    int subs(const string &s, int k) {\n        int n = s.length(); \n        int cnt = 0;\n        for (int i = 0; i < n; ++i) {\n            for (int j = i + 1; j <= n; ++j) {\n\n                string x = s.substr(i, j - i);\n                unordered_map<char, int>mp;\n                for(auto ch: x)\n                    mp[ch]++;\n                for(auto el: mp)\n                {\n                    if(el.second >= k)\n                    {\n                        cnt++;\n                        break;\n                    }\n                }\n            }\n        }\n        return cnt;\n    }\n    int numberOfSubstrings(string s, int k) {\n        return subs(s, k);\n    }\n};\n```\n# Optimal Using Sliding Window\n```cpp []\nclass Solution {\npublic:\n    int numberOfSubstrings(string s, int k) {\n        int n = s.length();\n        int ans = 0;\n        unordered_map<char, int> mp;\n        int i = 0;\n        for (int j = 0; j < n; ++j) {\n            mp[s[j]]++;\n            while (mp[s[j]] >= k) {\n                ans += (n - j);\n                mp[s[i]]--;\n                i++;\n            }\n        }\n        \n        return ans;\n    }\n};\n\n```
['C++']