Leaderboard
Showing:
Handle Score
@Ibrahim Nash 6560
@blackshadows 6400
@mb1973 5777
@Quandray 5271
@akhayrutdinov 5111
@saiujwal13083 5074
@kirtidee18 4356
@sanjay05 3762
@mantu_singh 3638
@gfgaccount 3601
@marius_valentin_dragoi 3525
Complete Leaderboard
Count of substrings containing K ones
Medium Accuracy: 72.07% Submissions: 629 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|)

Constraints:
1 ≤ N ≤ 105
1 ≤ K ≤ 104

to report an issue on this page.

Editorial

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

Yes

All Submissions

My Submissions:

Login to access your submissions.

Count of substrings containing K ones

Output Window