Count binary strings of length same as given string after removal of substrings “01” and “00” that consists of at least one ‘1’
Last Updated :
05 May, 2021
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: 7
Explanation: 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: 3
Explanation: 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 2N-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 2count – 1 as the required answer.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void countString(string S)
{
long long count = 0;
for ( auto it : S) {
if (it == '0' and count > 0) {
count--;
}
else {
count++;
}
}
cout << ((1 << count) - 1) << "\n" ;
}
int main()
{
string S = "1001" ;
countString(S);
return 0;
}
|
Java
import java.util.*;
class GFG{
static void countString(String S)
{
int count = 0 ;
for ( char it : S.toCharArray())
{
if (it == '0' && count > 0 )
{
count--;
}
else
{
count++;
}
}
System.out.print(( 1 << count) - 1 );
}
public static void main(String[] args)
{
String S = "1001" ;
countString(S);
}
}
|
Python3
def countString(S):
count = 0
for i in S:
if (i = = '0' and
count > 0 ):
count - = 1
else :
count + = 1
print (( 1 << count) - 1 )
if __name__ = = "__main__" :
S = "1001"
countString(S)
|
C#
using System;
class GFG{
static void countString( string S)
{
int count = 0;
foreach ( char it in S)
{
if (it == '0' && count > 0)
{
count--;
}
else
{
count++;
}
}
Console.Write((1 << count) - 1);
}
public static void Main( string [] args)
{
string S = "1001" ;
countString(S);
}
}
|
Javascript
<script>
function countString( S) {
var count = 0;
for ( var it =0;it< S.length; it++) {
if (S[it] == '0' && count > 0) {
count--;
} else {
count++;
}
}
document.write((1 << count) - 1);
}
var S = "1001" ;
countString(S);
</script>
|
Time Complexity: O(L) where L is the length of the string.
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...