Count of non-overlapping sub-strings "101" and "010" in the given binary string Last Updated : 07 Oct, 2022 Comments Improve Suggest changes Like Article Like Report Given binary string str, the task is to find the count of non-overlapping sub-strings of either the form "010" or "101". Examples: Input: str = "10101010101" Output: 3 str[0..2] = "101" str[3..5] = "010" str[6..8] = "101"Input: str = "111111111111110" Output: 0 Approach: Initialize 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 it's 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: C++ // C++ implementation of the approach #include <iostream> using namespace std; // Function to return the count of // required non-overlapping sub-strings int countSubStr(string &s, int n) { // To store the required count int count = 0; for (int i = 0; i < n - 2;) { // If "010" matches the sub-string // starting at current index i if (s[i] == '0' && s[i + 1] == '1' && s[i + 2] == '0') { count++; i += 3; } // If "101" matches the sub-string // starting at current index i else if (s[i] == '1' && s[i + 1] == '0' && s[i + 2] == '1') { count++; i += 3; } else { i++; } } return count; } // Driver code int main() { string s = "10101010101"; int n = s.length(); cout << countSubStr(s, n); return 0; } Java // Java implementation of the approach class GFG { // Function to return the count of // required non-overlapping sub-strings static int countSubStr(char[] s, int n) { // To store the required count int count = 0; for (int i = 0; i < n - 2;) { // If "010" matches the sub-string // starting at current index i if (s[i] == '0' && s[i + 1] == '1' && s[i + 2] == '0') { count++; i += 3; } // If "101" matches the sub-string // starting at current index i else if (s[i] == '1' && s[i + 1] == '0' && s[i + 2] == '1') { count++; i += 3; } else { i++; } } return count; } // Driver code public static void main(String[] args) { char[] s = "10101010101".toCharArray(); int n = s.length; System.out.println(countSubStr(s, n)); } } // This code is contributed by 29AjayKumar Python3 # Python3 implementation of the approach # Function to return the count of # required non-overlapping sub-strings def countSubStr(s, n) : # To store the required count count = 0; i = 0 while i < (n-2) : # If "010" matches the sub-string # starting at current index i if (s[i] == '0' and s[i + 1] == '1'and s[i + 2] == '0') : count += 1; i += 3; # If "101" matches the sub-string # starting at current index i elif (s[i] == '1' and s[i + 1] == '0'and s[i + 2] == '1') : count += 1; i += 3; else : i += 1; return count; # Driver code if __name__ == "__main__" : s = "10101010101"; n = len(s); print(countSubStr(s, n)); # This code is contributed by AnkitRai01 C# // C# implementation of the approach using System; class GFG { // Function to return the count of // required non-overlapping sub-strings static int countSubStr(char[] s, int n) { // To store the required count int count = 0; for (int i = 0; i < n - 2;) { // If "010" matches the sub-string // starting at current index i if (s[i] == '0' && s[i + 1] == '1' && s[i + 2] == '0') { count++; i += 3; } // If "101" matches the sub-string // starting at current index i else if (s[i] == '1' && s[i + 1] == '0' && s[i + 2] == '1') { count++; i += 3; } else { i++; } } return count; } // Driver code public static void Main(String[] args) { char[] s = "10101010101".ToCharArray(); int n = s.Length; Console.WriteLine(countSubStr(s, n)); } } // This code is contributed by Rajput-Ji JavaScript <script> // javascript implementation of the approach // Function to return the count of // required non-overlapping sub-strings function countSubStr( s , n) { // To store the required count var count = 0; for (i = 0; i < n - 2;) { // If "010" matches the sub-string // starting at current index i if (s[i] == '0' && s[i + 1] == '1' && s[i + 2] == '0') { count++; i += 3; } // If "101" matches the sub-string // starting at current index i else if (s[i] == '1' && s[i + 1] == '0' && s[i + 2] == '1') { count++; i += 3; } else { i++; } } return count; } // Driver code var s = "10101010101"; var n = s.length; document.write(countSubStr(s, n)); // This code contributed by Rajput-Ji </script> Output: 3 Time Complexity: O(n), where n is the length of the string.Auxiliary Space: O(1) as constant extra space is used Comment More infoAdvertise with us Next Article Count of non-overlapping sub-strings "101" and "010" in the given binary string A ayushgoyal Follow Improve Article Tags : Misc Strings DSA binary-string substring +1 More Practice Tags : MiscStrings Similar Reads Maximum count of unique index 10 or 01 substrings in given Binary string Given a binary string str of length N, the task is to count the maximum number of adjacent pairs of form "01" or "10" that can be formed from the given binary string when one character can be considered for only one pair. Note: Adjacent pair means pair formed using adjacent characters. Examples: Inp 5 min read Count of substrings that start and end with 1 in given Binary String Given a binary string, count the number of substrings that start and end with 1. Examples: Input: "00100101"Output: 3Explanation: three substrings are "1001", "100101" and "101" Input: "1001"Output: 1Explanation: one substring "1001" Recommended PracticeCount SubstringsTry It!Count of substrings tha 12 min read Count of substrings with equal ratios of 0s and 1s till ith index in given Binary String Given a binary string S, the task is to print the maximum number of substrings with equal ratios of 0s and 1s till the ith index from the start. Examples: Input: S = "110001"Output: {1, 2, 1, 1, 1, 2}Explanation: The given string can be partitioned into the following equal substrings: Valid substrin 9 min read Count of possible distinct Binary strings after replacing "11" with "0" Given a binary string str of size N containing 0 and 1 only, the task is to count all possible distinct binary strings when a substring "11" can be replaced by "0". Examples: Input: str = "11011"Output: 4Explanation: All possible combinations are "11011", "0011", "1100", "000". Input: str = "1100111 15 min read Maximize non-overlapping Substrings by splitting String into groups with K or (K+1) 1s Given a binary string S of length N and an integer K, the task is to find the maximum number of non-overlapping substrings that can be obtained by splitting it as per the following conditions: Each substring of the string contains K or K+1 number of 1. At most consecutive K substrings can contain th 10 min read Like