Count of substrings containing K ones
Medium Accuracy: 71.67% Submissions: 808 Points: 4

Given a binary string S and an integer K, the task is to count the number of substrings that contain exactly K ones.

Example 1:

Input : S = “10010”, K = 1
Output : 9
Explanation: The 9 substrings containing 
one 1 are, “1”, “10”, “100”, “001”, “01”,
“1”, “10”, “0010” and “010”.

Example 2:

Input : S = “111”, K = 2 
Output : 2 
Explanation: The 2 substrings containing
two 1 are “11”, “11”.

Your Task:  
You don't need to read input or print anything. Your task is to complete the function countOfSubstringWithKOnes() which takes the string S and an integer as inputs and returns the count.

Expected Time Complexity: O(|S|)
Expected Auxiliary Space: O(|S|)

1 ≤ N ≤ 105
1 ≤ K ≤ 104

We are replacing the old Disqus forum with the new Discussions section given below.
Click here to view old Disqus comments.

to report an issue on this page.


We strongly recommend solving this problem on your own before viewing its editorial. Do you still want to view the editorial?


All Submissions

My Submissions:

Login to access your submissions.

Count of substrings containing K ones

Output Window