# Count binary strings of length same as given string after removal of substrings “01” and “00” that consists of at least one ‘1’

Given a binary string **S**, the task is to count total binary strings consisting of at least one **‘1’** of length equal to the length of the given string after repeatedly removing all occurrences of substrings **“10”** and **“00” **from the given string.

**Examples:**

Input:S = “111”Output:7Explanation:Since there are no occurrences of “10” or “01” in the given string, length of the remaining string is 3. All possible binary strings of length 3 consisting of at least one set bit are {“001”, “010”, “011”, “100”, “101”, “110”, “111”}

Input:S = “0101”Output:3Explanation:After deleting “10”, S = “01”. Therefore, length of remaining string is 2. Strings of length 2 consisting of at least one set bit are {“01”, “10”, “11”}

**Approach:** The idea is to calculate the length of the given string after removing all substrings of the form **“10”** and **“00”** from it. Considering the remaining; length of the string to be **N**, the total number of strings consisting of at least one set bit will be equal to **2 ^{N}-1**.

Follow the below steps to solve the problem:

- Initialize a variable, say
**count**. - Iterate over the characters of the string
**S**. For each character, check if it is**‘0’**and the**count**is greater than**0**or not. If found to be true, decrement the count by**1**. - Otherwise, if the current character is
**‘1’**, increment**count**by**1**. - After complete traversal of the string, print
**2**as the required answer.^{count}– 1

Below is the implementation of the above approach:

## C++

`// C++ program for the above approach` `#include <bits/stdc++.h>` `using` `namespace` `std;` `// Function to count the strings` `// consisting of at least 1 set bit` `void` `countString(string S)` `{` ` ` `// Initialize count` ` ` `long` `long` `count = 0;` ` ` `// Iterate through string` ` ` `for` `(` `auto` `it : S) {` ` ` `if` `(it == ` `'0'` `and count > 0) {` ` ` `count--;` ` ` `}` ` ` `else` `{` ` ` `count++;` ` ` `}` ` ` `}` ` ` `// The answer is 2^N-1` ` ` `cout << ((1 << count) - 1) << ` `"\n"` `;` `}` `// Driver Code` `int` `main()` `{` ` ` `// Given string` ` ` `string S = ` `"1001"` `;` ` ` `// Function call` ` ` `countString(S);` ` ` `return` `0;` `}` |

## Java

`// Java program for the above approach` `import` `java.util.*;` ` ` `class` `GFG{` ` ` `// Function to count the strings` `// consisting of at least 1 set bit` `static` `void` `countString(String S)` `{` ` ` ` ` `// Initialize count` ` ` `int` `count = ` `0` `;` ` ` ` ` `// Iterate through string` ` ` `for` `(` `char` `it : S.toCharArray())` ` ` `{` ` ` `if` `(it == ` `'0'` `&& count > ` `0` `)` ` ` `{` ` ` `count--;` ` ` `}` ` ` `else` ` ` `{` ` ` `count++;` ` ` `}` ` ` `}` ` ` ` ` `// The answer is 2^N-1` ` ` `System.out.print((` `1` `<< count) - ` `1` `);` `}` ` ` `// Driver Code` `public` `static` `void` `main(String[] args)` `{` ` ` ` ` `// Given string` ` ` `String S = ` `"1001"` `;` ` ` ` ` `// Function call` ` ` `countString(S);` `}` `}` `// This code is contributed by susmitakundugoaldanga` |

## Python3

`# Python3 program for the` `# above approach ` `# Function to count the` `# strings consisting of` `# at least 1 set bit` `def` `countString(S):` ` ` ` ` `# Initialize count` ` ` `count ` `=` `0` ` ` ` ` `# Iterate through` ` ` `# string` ` ` `for` `i ` `in` `S:` ` ` `if` `(i ` `=` `=` `'0'` `and` ` ` `count > ` `0` `): ` ` ` `count ` `-` `=` `1` ` ` ` ` `else` `: ` ` ` `count ` `+` `=` `1` ` ` `# The answer is 2^N-1 ` ` ` `print` `((` `1` `<< count) ` `-` `1` `) ` `# Driver Code` `if` `__name__ ` `=` `=` `"__main__"` `:` ` ` `# Given string ` ` ` `S ` `=` `"1001"` ` ` ` ` `# Function call ` ` ` `countString(S)` `# This code is contributed by Virusbuddah_` |

## C#

`// C# program for the above approach` `using` `System;` ` ` `class` `GFG{` ` ` `// Function to count the strings` `// consisting of at least 1 set bit` `static` `void` `countString(` `string` `S)` `{` ` ` ` ` `// Initialize count` ` ` `int` `count = 0;` ` ` ` ` `// Iterate through string` ` ` `foreach` `(` `char` `it ` `in` `S)` ` ` `{` ` ` `if` `(it == ` `'0'` `&& count > 0)` ` ` `{` ` ` `count--;` ` ` `}` ` ` `else` ` ` `{` ` ` `count++;` ` ` `}` ` ` `}` ` ` ` ` `// The answer is 2^N-1` ` ` `Console.Write((1 << count) - 1);` `}` ` ` `// Driver Code` `public` `static` `void` `Main(` `string` `[] args)` `{` ` ` ` ` `// Given string` ` ` `string` `S = ` `"1001"` `;` ` ` ` ` `// Function call` ` ` `countString(S);` `}` `}` `// This code is contributed by chitranayal` |

## Javascript

`<script>` `// javascript program for the above approach` ` ` `// Function to count the strings` ` ` `// consisting of at least 1 set bit` ` ` `function` `countString( S) {` ` ` `// Initialize count` ` ` `var` `count = 0;` ` ` `// Iterate through string` ` ` `for` `(` `var` `it =0;it< S.length; it++) {` ` ` `if` `(S[it] == ` `'0'` `&& count > 0) {` ` ` `count--;` ` ` `} ` `else` `{` ` ` `count++;` ` ` `}` ` ` `}` ` ` `// The answer is 2^N-1` ` ` `document.write((1 << count) - 1);` ` ` `}` ` ` `// Driver Code` ` ` ` ` `// Given string` ` ` `var` `S = ` `"1001"` `;` ` ` `// Function call` ` ` `countString(S);` `// This code contributed by umadevi9616` `</script>` |

**Output:**

3

**Time Complexity: **O(L) where L is the length of the 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. To complete your preparation from learning a language to DS Algo and many more, please refer **Complete Interview Preparation Course****.**

In case you wish to attend **live classes **with experts, please refer **DSA Live Classes for Working Professionals **and **Competitive Programming Live for Students**.