Given a binary string of length N, we need to find out how many sub-strings of this string contains only 1s.
Input: S = “0110111”
There are 9 substring with only 1’s characters.
“1” comes 5 times.
“11” comes 3 times.
“111” comes 1 time.
Input: S = “000”
Approach: The idea is to traverse the binary string and count the consecutive one’s in the string. Below is the illustration of the approach:
- Traverse the given binary string from index 0 to length – 1
- Count the number of consecutive “1” till index i.
- For each new character str[i], there will be more substring with all character’s as “1”
Below is the implementation of the above approach:
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 containing only the given character
- Count of substrings of a binary string containing K ones
- Count of Subsets containing only the given value K
- Longest Subsequence of a String containing only vowels
- Longest Subsequence of a String containing only Consonants
- Count ways to split a Binary String into three substrings having equal count of zeros
- Minimize length of Substrings containing at least one common Character
- Count of substrings consisting only of vowels
- Count of substrings formed using a given set of characters only
- Lexicographically smallest substring with maximum occurrences containing a's and b's only
- Check if binary representations of 0 to N are present as substrings in given binary string
- 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
- Given a binary string, count number of substrings that start and end with 1.
- Count binary strings of length same as given string after removal of substrings "01" and "00" that consists of at least one '1'
- Build a DFA to accept a binary string containing "01" i times and "1" 2j times
- 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
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 Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.