Given a binary string of length N and an integer K, we need to find out how many substrings of this string are exist which contains exactly K ones.
Input : s = “10010” K = 1 Output : 9 The 9 substrings containing one 1 are, “1”, “10”, “100”, “001”, “01”, “1”, “10”, “0010” and “010”
In this problem we need to find count of substrings which contains exactly K ones or in other words sum of digits in those substring is K. We first create a prefix sum array and loop over that and stop when sum value is greater than or equal to K. Now if sum at current index is (K + a) then we know that substring sum, from all those indices where sum is (a), till current index will be K, so count of indices having sum (a), will be added to result. This procedure is explained with an example below,
string s = “100101” K = 2 prefix sum array = [1, 1, 1, 2, 2, 3] So, at index 3, we have prefix sum 2, Now total indices from where sum is 2, is 1 so result = 1 Substring considered = [“1001”] At index 4, we have prefix sum 2, Now total indices from where sum is 2, is 1 so result = 2 Substring considered = [“1001”, “10010”] At index 5, we have prefix sum 3, Now total indices from where sum is 2, is 3 so result = 5 Substring considered = [“1001”, “10010”, “00101”, “0101”, “101”]
So we need to track two things, prefix sum and frequency of particular sum. In below code, instead of storing complete prefix sum, only prefix sum at current index is stored using one variable and frequency of sums in stored in an array. Total time complexity of solution is O(N).
This article is contributed by Utkarsh Trivedi. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Count of substrings of a Binary string containing only 1s
- Count of substrings containing only the given character
- Maximum difference of zeros and ones in binary string
- Maximum difference of zeros and ones in binary string | Set 2 (O(n) time)
- Length of longest consecutive ones by at most one swap in a Binary String
- Minimize length of Substrings containing at least one common Character
- Count ways to split a Binary String into three substrings having equal count of zeros
- Check if binary representations of 0 to N are present as substrings in given binary string
- Given a binary string, count number of substrings that start and end with 1.
- Count of substrings of a given Binary string with all characters same
- Check if all substrings of length K of a Binary String has equal count of 0s and 1s
- Build a DFA to accept a binary string containing "01" i times and "1" 2j times
- Queries to answer the number of ones and zero to the left of given index
- Check whether two strings can be made equal by copying their characters with the adjacent ones
- Maximum number of strings that can be formed with given zeros and ones
- Find the smallest window in a string containing all characters of another string
- String containing first letter of every word in a given string with spaces
- Lexicographic smallest permutation of a String containing the second String as a Substring
- Lexicographically smallest and largest anagrams of a string containing another string as its substring
- Minimize length of prefix of string S containing all characters of another string T