# Count of non-overlapping sub-strings “101” and “010” in the given binary string

Given a 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

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

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:

## C++

 `// C++ implementation of the approach ` `#include ` `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 `

Output:

```3
```

My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.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.

Article Tags :
Practice Tags :

1

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.