Given binary string str containing only 0 and 1, the task is to find the number of sub-strings containing only 1s and 0s respectively, i.e all characters same.
Input: str = “011”
Three sub-strings are “1“, “1”, “11” which have only 1 in them, and one substring is there which contains only “0”.
Input: str = “0000”
There are no sub-strings having all ones in it.
Naive Approach: The idea is to generate all possible sub-string of the given string. For each sub-string check if the string contains all 1’s or all 0’s. If yes then count that sub-string. Print the count of sub-string after the above operations.
Time Complexity: O(N3)
Auxiliary Space: O(N)
- Initialize two pointers say L and R and initialize them to 0.
- Now iterate in the given string and check if the current character is equal to 1 or not. If it is, then extend the window by incrementing the value of R.
- If the current character is 0, then the window L to R – 1 contains all ones.
- Add the number of sub-strings of L to R – 1 to the answer that is ((R – L) * (R – L + 1)) / 2 and increment R and reinitialize L as R.
- Repeat the process until L and R cross each other.
- Print the count of all the substring in step 4.
- To count the number of substring with all 0s flip the given string i.e., all 0s will be converted into 1s and vice-versa.
- Repeat the above steps from step 1 to step 4 for character 1 in the flipped string to get a count of the substring that contains only 0s in it and print the count.
Below is the implementation of the above approach:
Time Complexity: O(N), where N is the length of the given string.
Auxiliary Space: O(1)
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 number of substrings of a string consisting of same characters
- Count substrings with same first and last characters
- Recursive solution to count substrings with same first and last characters
- Count of Substrings with at least K pairwise Distinct Characters having same Frequency
- Min flips of continuous characters to make all characters same in a string
- Check if all substrings of length K of a Binary String has equal count of 0s and 1s
- 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
- Count of all unique substrings with non-repeating characters
- Count of substrings having all distinct characters
- Minimum characters to be replaced to make frequency of all characters same
- String with k distinct characters and no same characters adjacent
- Check if a palindromic string can be obtained by concatenating substrings split from same indices of two given strings
- Given a binary string, count number of substrings that start and end with 1.
- Count of Substrings that can be formed without using the given list of Characters
- Count of substrings formed using a given set of characters only
- Remove minimum characters from string to split it into three substrings under given constraints
- Calculate Sum of ratio of special characters to length of substrings of the given string
- Count the number of vowels occurring in all the substrings of given string
- Sum of all distances between occurrences of same characters in a given string
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. 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.