# Count of sub-strings with equal consecutive 0’s and 1’s

Given a binary string str of 0’s and 1’s only. The task is to count the total numbers of substrings of string str such that each substring has an equal number of consecutive 0’s and 1’s in it.

Examples

Input: str = “010011”
Output: 4
Explanation:
The substrings with consecutive 0’s and 1’s are “01”, “10”, “0011”, “01”. Hence, the count is 4.
Note:
The two “01” are at different positions: [0, 1] and [3, 4].
“010011” has the same number of 0’s and 1’s but they are not consecutive.

Input: str = “0001110010”
Output: 7
Explanation:
The substrings with consecutive 0’s and 1’s are “000111”, “0011”, “01”, “1100”, “10”, “01”, “10”.

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

Approach:

• Count the number of consecutive 0’s (or 1’s) from start of the string.
• Then count the number of consecutive 1’s (or 0’s) from the position in the string str where count of 0’s (or 1’s) ends.
• The total number of substrings with consecutive 0’s and 1’s is the minimum of the count of consecutive 0’s and 1’s found in above two steps.
• Repeat the above steps till the end of the string str.

Below is the implementation of the above approach:

## C++

 `// C++ implementation of the ` `// above approach ` `#include ` `using` `namespace` `std; ` ` `  `// Function to find the count ` `// of substrings with equal no. ` `// of consecutive 0's and 1's ` `int` `countSubstring(string& S, ``int``& n) ` `{ ` `    ``// To store the total count ` `    ``// of substrings ` `    ``int` `ans = 0; ` ` `  `    ``int` `i = 0; ` ` `  `    ``// Traversing the string ` `    ``while` `(i < n) { ` ` `  `        ``// Count of consecutive ` `        ``// 0's & 1's ` `        ``int` `cnt0 = 0, cnt1 = 0; ` ` `  `        ``// Counting subarrays of ` `        ``// type "01" ` `        ``if` `(S[i] == ``'0'``) { ` ` `  `            ``// Count the consecutive ` `            ``// 0's ` `            ``while` `(i < n && S[i] == ``'0'``) { ` `                ``cnt0++; ` `                ``i++; ` `            ``} ` ` `  `            ``// If consecutive 0's ` `            ``// ends then check for ` `            ``// consecutive 1's ` `            ``int` `j = i; ` ` `  `            ``// Counting consecutive 1's ` `            ``while` `(j < n && S[j] == ``'1'``) { ` `                ``cnt1++; ` `                ``j++; ` `            ``} ` `        ``} ` ` `  `        ``// Counting subarrays of ` `        ``// type "10" ` `        ``else` `{ ` ` `  `            ``// Count consecutive 1's ` `            ``while` `(i < n && S[i] == ``'1'``) { ` `                ``cnt1++; ` `                ``i++; ` `            ``} ` ` `  `            ``// If consecutive 1's ` `            ``// ends then check for ` `            ``// consecutive 0's ` `            ``int` `j = i; ` ` `  `            ``// Count consecutive 0's ` `            ``while` `(j < n && S[j] == ``'0'``) { ` `                ``cnt0++; ` `                ``j++; ` `            ``} ` `        ``} ` ` `  `        ``// Update the total count ` `        ``// of substrings with ` `        ``// minimum of (cnt0, cnt1) ` `        ``ans += min(cnt0, cnt1); ` `    ``} ` ` `  `    ``// Return answer ` `    ``return` `ans; ` `} ` ` `  `// Driver code ` `int` `main() ` `{ ` `    ``string S = ``"0001110010"``; ` `    ``int` `n = S.length(); ` ` `  `    ``// Function to print the ` `    ``// count of substrings ` `    ``cout << countSubstring(S, n); ` `    ``return` `0; ` `} `

## Java

 `// Java implementation of the  ` `// above approach  ` `class` `GFG{ ` ` `  `    ``// Function to find the count  ` `    ``// of substrings with equal no.  ` `    ``// of consecutive 0's and 1's  ` `    ``static` `int` `countSubstring(String S, ``int` `n)  ` `    ``{  ` `        ``// To store the total count  ` `        ``// of substrings  ` `        ``int` `ans = ``0``;  ` `     `  `        ``int` `i = ``0``;  ` `     `  `        ``// Traversing the string  ` `        ``while` `(i < n) {  ` `     `  `            ``// Count of consecutive  ` `            ``// 0's & 1's  ` `            ``int` `cnt0 = ``0``, cnt1 = ``0``;  ` `     `  `            ``// Counting subarrays of  ` `            ``// type "01"  ` `            ``if` `(S.charAt(i) == ``'0'``) {  ` `     `  `                ``// Count the consecutive  ` `                ``// 0's  ` `                ``while` `(i < n && S.charAt(i) == ``'0'``) {  ` `                    ``cnt0++;  ` `                    ``i++;  ` `                ``}  ` `     `  `                ``// If consecutive 0's  ` `                ``// ends then check for  ` `                ``// consecutive 1's  ` `                ``int` `j = i;  ` `     `  `                ``// Counting consecutive 1's  ` `                ``while` `(j < n && S.charAt(j) == ``'1'``) {  ` `                    ``cnt1++;  ` `                    ``j++;  ` `                ``}  ` `            ``}  ` `     `  `            ``// Counting subarrays of  ` `            ``// type "10"  ` `            ``else` `{  ` `     `  `                ``// Count consecutive 1's  ` `                ``while` `(i < n && S.charAt(i) == ``'1'``) {  ` `                    ``cnt1++;  ` `                    ``i++;  ` `                ``}  ` `     `  `                ``// If consecutive 1's  ` `                ``// ends then check for  ` `                ``// consecutive 0's  ` `                ``int` `j = i;  ` `     `  `                ``// Count consecutive 0's  ` `                ``while` `(j < n && S.charAt(j) == ``'0'``) {  ` `                    ``cnt0++;  ` `                    ``j++;  ` `                ``}  ` `            ``}  ` `     `  `            ``// Update the total count  ` `            ``// of substrings with  ` `            ``// minimum of (cnt0, cnt1)  ` `            ``ans += Math.min(cnt0, cnt1);  ` `        ``}  ` `     `  `        ``// Return answer  ` `        ``return` `ans;  ` `    ``}  ` `     `  `    ``// Driver code  ` `    ``static` `public` `void` `main(String args[]) ` `    ``{  ` `        ``String S = ``"0001110010"``;  ` `        ``int` `n = S.length();  ` `     `  `        ``// Function to print the  ` `        ``// count of substrings  ` `        ``System.out.println(countSubstring(S, n));  ` `    ``}  ` `} ` ` `  `// This code is contributed by Yash_R `

## Python3

 `# Python3 implementation of the ` `# above approach ` ` `  `# Function to find the count ` `# of substrings with equal no. ` `# of consecutive 0's and 1's ` `def` `countSubstring(S, n) : ` ` `  `    ``# To store the total count ` `    ``# of substrings ` `    ``ans ``=` `0``; ` ` `  `    ``i ``=` `0``; ` ` `  `    ``# Traversing the string ` `    ``while` `(i < n) : ` ` `  `        ``# Count of consecutive ` `        ``# 0's & 1's ` `        ``cnt0 ``=` `0``; cnt1 ``=` `0``; ` ` `  `        ``# Counting subarrays of ` `        ``# type "01" ` `        ``if` `(S[i] ``=``=` `'0'``) : ` ` `  `            ``# Count the consecutive ` `            ``# 0's ` `            ``while` `(i < n ``and` `S[i] ``=``=` `'0'``) : ` `                ``cnt0 ``+``=` `1``; ` `                ``i ``+``=` `1``; ` ` `  `            ``# If consecutive 0's ` `            ``# ends then check for ` `            ``# consecutive 1's ` `            ``j ``=` `i; ` ` `  `            ``# Counting consecutive 1's ` `            ``while` `(j < n ``and` `S[j] ``=``=` `'1'``) : ` `                ``cnt1 ``+``=` `1``; ` `                ``j ``+``=` `1``; ` ` `  `        ``# Counting subarrays of ` `        ``# type "10" ` `        ``else` `: ` ` `  `            ``# Count consecutive 1's ` `            ``while` `(i < n ``and` `S[i] ``=``=` `'1'``) : ` `                ``cnt1 ``+``=` `1``; ` `                ``i ``+``=` `1``; ` ` `  `            ``# If consecutive 1's ` `            ``# ends then check for ` `            ``# consecutive 0's ` `            ``j ``=` `i; ` ` `  `            ``# Count consecutive 0's ` `            ``while` `(j < n ``and` `S[j] ``=``=` `'0'``) : ` `                ``cnt0 ``+``=` `1``; ` `                ``j ``+``=` `1``; ` ` `  `        ``# Update the total count ` `        ``# of substrings with ` `        ``# minimum of (cnt0, cnt1) ` `        ``ans ``+``=` `min``(cnt0, cnt1); ` ` `  `    ``# Return answer ` `    ``return` `ans; ` ` `  `# Driver code ` `if` `__name__ ``=``=` `"__main__"` `: ` `    ``S ``=` `"0001110010"``; ` `    ``n ``=` `len``(S); ` ` `  `    ``# Function to print the ` `    ``# count of substrings ` `    ``print``(countSubstring(S, n)); ` `     `  `# This code is contributed by Yash_R `

## C#

 `// C# implementation of the  ` `// above approach  ` `using` `System; ` ` `  `class` `GFG{ ` ` `  `    ``// Function to find the count  ` `    ``// of substrings with equal no.  ` `    ``// of consecutive 0's and 1's  ` `    ``static` `int` `countSubstring(``string` `S, ``int` `n)  ` `    ``{  ` `        ``// To store the total count  ` `        ``// of substrings  ` `        ``int` `ans = 0;  ` `     `  `        ``int` `i = 0;  ` `     `  `        ``// Traversing the string  ` `        ``while` `(i < n) {  ` `     `  `            ``// Count of consecutive  ` `            ``// 0's & 1's  ` `            ``int` `cnt0 = 0, cnt1 = 0;  ` `     `  `            ``// Counting subarrays of  ` `            ``// type "01"  ` `            ``if` `(S[i] == ``'0'``) {  ` `     `  `                ``// Count the consecutive  ` `                ``// 0's  ` `                ``while` `(i < n && S[i] == ``'0'``) {  ` `                    ``cnt0++;  ` `                    ``i++;  ` `                ``}  ` `     `  `                ``// If consecutive 0's  ` `                ``// ends then check for  ` `                ``// consecutive 1's  ` `                ``int` `j = i;  ` `     `  `                ``// Counting consecutive 1's  ` `                ``while` `(j < n && S[j] == ``'1'``) {  ` `                    ``cnt1++;  ` `                    ``j++;  ` `                ``}  ` `            ``}  ` `     `  `            ``// Counting subarrays of  ` `            ``// type "10"  ` `            ``else` `{  ` `     `  `                ``// Count consecutive 1's  ` `                ``while` `(i < n && S[i] == ``'1'``) {  ` `                    ``cnt1++;  ` `                    ``i++;  ` `                ``}  ` `     `  `                ``// If consecutive 1's  ` `                ``// ends then check for  ` `                ``// consecutive 0's  ` `                ``int` `j = i;  ` `     `  `                ``// Count consecutive 0's  ` `                ``while` `(j < n && S[j] == ``'0'``) {  ` `                    ``cnt0++;  ` `                    ``j++;  ` `                ``}  ` `            ``}  ` `     `  `            ``// Update the total count  ` `            ``// of substrings with  ` `            ``// minimum of (cnt0, cnt1)  ` `            ``ans += Math.Min(cnt0, cnt1);  ` `        ``}  ` `     `  `        ``// Return answer  ` `        ``return` `ans;  ` `    ``}  ` `     `  `    ``// Driver code  ` `    ``static` `public` `void` `Main () ` `    ``{  ` `        ``string` `S = ``"0001110010"``;  ` `        ``int` `n = S.Length;  ` `     `  `        ``// Function to print the  ` `        ``// count of substrings  ` `        ``Console.WriteLine(countSubstring(S, n));  ` `    ``}  ` `} ` ` `  `// This code is contributed by Yash_R `

Output:

```7
```

Time Complexity: O(N), where N = length of string.

Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.

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.

Improved By : Yash_R