DEV Community

Prashant Mishra
Prashant Mishra

Posted on

Count Substrings With K-Frequency Characters I

Problem

class Solution {
    public int numberOfSubstrings(String s, int k) {
        int left  =0, right = 0;
        int hash[] = new int[26];
        int count = 0;
        while(right<s.length()){
            char c= s.charAt(right);
            hash[c-'a']++;
            while(hash[c-'a']>=k){
                count = count+1 + s.length()-1-right;
                hash[s.charAt(left)-'a']--;
                left++;
            }
            right++;
        }
        return count;
    }
}
Enter fullscreen mode Exit fullscreen mode

Top comments (0)