Given a binary string str, the task is to find the count of non-overlapping sub-strings of either the form “010” or “101”.
Input: str = “10101010101”
str[0..2] = “101”
str[3..5] = “010”
str[6..8] = “101”
Input: str = “111111111111110”
Approach: Initialise count = 0 and for every index i in the given string check whether the sub-string of size 3 starting at the current index i matches either with “010” or “101”. If its a match then update count = count + 1 and i = i + 3 (to avoid overlapping of sub-strings) else increment i by 1.
Below is the implementation of the above approach:
GeeksforGeeks has prepared a complete interview preparation course with premium videos, theory, practice problems, TA support and many more features. Please refer Placement 100 for details
- Count of substrings of a binary string containing K ones
- Given a binary string, count number of substrings that start and end with 1.
- Counting even decimal value substrings in a binary string
- Number of substrings with odd decimal value in a binary string
- Split the binary string into substrings with equal number of 0s and 1s
- Queries to find the count of vowels in the substrings of the given string
- Count number of substrings of a string consisting of same characters
- Permutation of given string that maximizes count of Palindromic substrings
- Count of distinct substrings of a string using Suffix Trie
- Count the number of vowels occurring in all the substrings of given string
- Count of distinct substrings of a string using Suffix Array
- Count of 1-bit and 2-bit characters in the given binary string
- Count of cyclic permutations having XOR with other binary string as 0
- Count of operations to make a binary string"ab" free
- Maximize count of 0s in left and 1s in right substring by splitting given Binary 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.