Count Substrings With K-Frequency Characters I
Prashant Mishra

Prashant Mishra @prashantrmishra

About: There is always a price for those who persevere.

Location:
India
Joined:
Jul 2, 2022

Count Substrings With K-Frequency Characters I

Publish Date: Oct 20 '24
0 0

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

Comments 0 total

    Add comment