Simple Greedy & Sliding Window
Complexity
Time complexity:
O(n)
Space complexity:
O(1)Code

# Complexity
- Time complexity:
<!-- Add your time complexity here, e.g. $$O(n)$$ -->
O(n)
---
- Space complexity:
<!-- Add your space complexity here, e.g. $$O(n)$$ -->
O(1)
---
# Code
```cpp []
class Solution {
public:
    int numberOfSubstrings(string s, int k) {
        int frq[26]={0};
        int l=0, r=0, n=s.size(), ans=0;
        while(r<n){
            frq[s[r]-'a']++;
            while(frq[s[r]-'a']==k){
                ans +=n-r;
                frq[s[l]-'a']--;
                l++;
            }
            r++;
        }
        return ans;
    }
};
```
```java []
class Solution {
    public int numberOfSubstrings(String s, int k) {
        int[] frq = new int[26];
        int l=0, r=0, n=s.length(), ans=0;
        while(r<n){
            frq[s.charAt(r)-'a']++;
            while(frq[s.charAt(r)-'a']==k){
                ans +=n-r;
                frq[s.charAt(l)-'a']--;
                l++;
            }
            r++;
        }
        return ans;
    }
}
```
['Hash Table', 'String', 'Greedy', 'Sliding Window', 'C++', 'Java']