Print string after removing all (“10” or “01”) from the binary string
Last Updated :
24 Nov, 2021
Given a binary string str consisting of only 0's and 1's, the task is to print the string after removing the occurrences of "10" and "01" from the string one by one. Print -1 if the string becomes null.
Examples:
Input: str = "101100"
Output: -1
Explanation:
In the first step, "10" at index 0 and 1 is removed from the string.
101100 -> 1100
In the second step, "10" at index 1 and 2 is removed from the string.
1100 -> 10
Finally, "10" is removed and the string becomes empty.
10 -> NULL
Input: str = "010110100"
Output: 0
Explanation:
In the first step, "01" at index 0 and 1 is removed from the string.
010110100 -> 0110100
In the second step, "01" at index 0 and 1 is removed from the string.
0110100 -> 10100
In the third step, "10" at index 0 and 1 is removed from the string.
10100 -> 100
Finally, "10" is removed and the string becomes "0".
100 -> 0
Observation: On observing carefully, since the given string is a binary string, all the strings can be cleared except the extra 0's and 1's that are present in the string which can't get paired with its compliment. For example:
Let str = "010110100".
For this string, the number of 0's are 5 and the number of 1's are 4.
Now, let's start removing alternate substrings one by one:
- 010110100 -> 0110100
- 0110100 -> 10100
- 10100 -> 100
- 100 -> 0
At this point, the string cannot be further reduced.
Hence, from the above example, it can be visualized that the string can be reduced as long as there are 1's and 0's in the string.
We already discussed the approach to find the count of deletions of 0's and 1's in the Previous Article. Here, we have done a slight modification in the previous approach to generate the remaining string after all possible deletions.
Approach: From the above observation, it can be concluded that the final strings contain only the extra 1's or 0's that cannot be paired with any of the digits in the string. Therefore, the idea to solve this problem is to count the number of 0's and 1's in the string and find the difference between the two counts. This count signifies the number of remaining 1's or 0's based on whichever value is higher.
The following steps can be followed to compute the answer:
- Get the count of 0's present in the string and store it in a variable.
- Get the count of 1's present in the string and store it in another variable.
- If the count of 1's is equal to 0's, then the entire string can be reduced. Therefore, return -1.
- If the count of 1's is greater than the count of 0's, then finally those many 1's get remained and further reduction would not be possible. Therefore, append those many 1's to an empty string and return the string.
- Similarly, if the count of 0's is greater than the count of 1's, then find the difference and append those many 0's to an empty string and return it.
Below is the implementation of the above approach:
C++
// C++ program to print the final string
// after removing all the occurrences of
// "10" and "01" from the given binary string
#include <bits/stdc++.h>
using namespace std;
// Function to print the final string
// after removing all the occurrences of
// "10" and "01" from the given binary string
void finalString(string str)
{
// Variables to store the
// count of 1's and 0's
int x = 0, y = 0;
// Variable left will store
// whether 0's or 1's is left
// in the final string
int left;
// Length of the string
int n = str.length();
// For loop to count the occurrences
// of 1's and 0's in the string
for (int i = 0; i < n; i++) {
if (str[i] == '1')
x++;
else
y++;
}
// To check if the count of 1's is
// greater than the count of 0's or not.
// If x is greater, then those many 1's
// are printed.
if (x > y)
left = 1;
else
left = 0;
// Length of the final remaining string
// after removing all the occurrences
int length = n - 2 * min(x, y);
// Printing the final string
for (int i = 0; i < length; i++) {
cout << left;
}
}
// Driver Code
int main()
{
string str = "010110100100000";
finalString(str);
return 0;
}
Java
// Java program to print the final String
// after removing all the occurrences of
// "10" and "01" from the given binary String
import java.util.*;
class GFG{
// Function to print the final String
// after removing all the occurrences of
// "10" and "01" from the given binary String
static void finalString(String str)
{
// Variables to store the
// count of 1's and 0's
int x = 0, y = 0;
// Variable left will store
// whether 0's or 1's is left
// in the final String
int left;
// Length of the String
int n = str.length();
// For loop to count the occurrences
// of 1's and 0's in the String
for (int i = 0; i < n; i++) {
if (str.charAt(i) == '1')
x++;
else
y++;
}
// To check if the count of 1's is
// greater than the count of 0's or not.
// If x is greater, then those many 1's
// are printed.
if (x > y)
left = 1;
else
left = 0;
// Length of the final remaining String
// after removing all the occurrences
int length = n - 2 * Math.min(x, y);
// Printing the final String
for (int i = 0; i < length; i++) {
System.out.print(left);
}
}
// Driver Code
public static void main(String[] args)
{
String str = "010110100100000";
finalString(str);
}
}
// This code is contributed by sapnasingh4991
Python3
# Python 3 program to print the final string
# after removing all the occurrences of
# "10" and "01" from the given binary string
# Function to print the final string
# after removing all the occurrences of
# "10" and "01" from the given binary string
def finalString(st):
# Variables to store the
# count of 1's and 0's
x , y = 0 , 0
# Length of the string
n = len(st)
# For loop to count the occurrences
# of 1's and 0's in the string
for i in range( n):
if (st[i] == '1'):
x += 1
else:
y += 1
# To check if the count of 1's is
# greater than the count of 0's or not.
# If x is greater, then those many 1's
# are printed.
if (x > y):
left = 1
else:
left = 0
# Length of the final remaining string
# after removing all the occurrences
length = n - 2 * min(x, y);
# Printing the final string
for i in range(length):
print(left, end="")
# Driver Code
if __name__ == "__main__":
st = "010110100100000"
finalString(st)
# This code is contributed by chitranayal
C#
// C# program to print the readonly String
// after removing all the occurrences of
// "10" and "01" from the given binary String
using System;
class GFG{
// Function to print the readonly String
// after removing all the occurrences of
// "10" and "01" from the given binary String
static void finalString(String str)
{
// Variables to store the
// count of 1's and 0's
int x = 0, y = 0;
// Variable left will store
// whether 0's or 1's is left
// in the readonly String
int left;
// Length of the String
int n = str.Length;
// For loop to count the occurrences
// of 1's and 0's in the String
for (int i = 0; i < n; i++) {
if (str[i] == '1')
x++;
else
y++;
}
// To check if the count of 1's is
// greater than the count of 0's or not.
// If x is greater, then those many 1's
// are printed.
if (x > y)
left = 1;
else
left = 0;
// Length of the readonly remaining String
// after removing all the occurrences
int length = n - 2 * Math.Min(x, y);
// Printing the readonly String
for (int i = 0; i < length; i++) {
Console.Write(left);
}
}
// Driver Code
public static void Main(String[] args)
{
String str = "010110100100000";
finalString(str);
}
}
// This code is contributed by 29AjayKumar
JavaScript
<script>
// Javascript program to print the final String
// after removing all the occurrences of
// "10" and "01" from the given binary String
// Function to print the final String
// after removing all the occurrences of
// "10" and "01" from the given binary String
function finalString(str)
{
// Variables to store the
// count of 1's and 0's
let x = 0, y = 0;
// Variable left will store
// whether 0's or 1's is left
// in the final String
let left;
// Length of the String
let n = str.length;
// For loop to count the occurrences
// of 1's and 0's in the String
for (let i = 0; i < n; i++) {
if (str[i] == '1')
x++;
else
y++;
}
// To check if the count of 1's is
// greater than the count of 0's or not.
// If x is greater, then those many 1's
// are printed.
if (x > y)
left = 1;
else
left = 0;
// Length of the final remaining String
// after removing all the occurrences
let length = n - 2 * Math.min(x, y);
// Printing the final String
for (let i = 0; i < length; i++) {
document.write(left);
}
}
// Driver Code
let str = "010110100100000";
finalString(str);
</script>
Time Complexity Analysis:
- The for loop to count the occurrences of 1's and 0's take O(N) time where N is the length of the string.
- The if statement takes constant time. So the time complexity for the if statement is O(1).
- The loop to print the final string takes O(N) in the worst case when the entire string is only 0's or 1's.
- Therefore, the overall time complexity is O(N).
Similar Reads
Minimum steps to remove substring 010 from a binary string
Given a binary string, the task is to count the minimum steps to remove substring "010" from this binary string. Examples: Input: binary_string = "0101010" Output: 2 Switching 0 to 1 at index 2 and index 4 will remove the substring 010. Hence the number of steps needed is 2. Input: binary_string = "
4 min read
Find Binary string by converting all 01 or 10 to 11 after M iterations
Given a binary string str[] of size N and an integer M. This binary string can be modified by flipping all the 0's to 1 which have exactly one 1 as a neighbour. The task is to find the final state of the binary string after M such iterations.Note: 2?N?103, 1?M?109 Examples: Input: str="01100", M=1Ou
8 min read
Smallest string obtained by removing all occurrences of 01 and 11 from Binary String
Given a binary string S , the task is to find the smallest string possible by removing all occurrences of substrings "01" and "11". After removal of any substring, concatenate the remaining parts of the string. Examples: Input: S = "0010110"Output:Length = 1 String = 0Explanation: String can be tran
7 min read
Smallest string obtained by removing all occurrences of 01 and 11 from Binary String | Set 2
Given a binary string S of length N, the task is to find the smallest string possible by removing all occurrences of substrings â01â and â11â. After removal of any substring, concatenate the remaining parts of the string. Examples: Input: S = "1010"Output: 2Explanation: Removal of substring "01" mod
5 min read
Most frequent character in a string after replacing all occurrences of X in a Binary String
Given a string S of length N consisting of 1, 0, and X, the task is to print the character ('1' or '0') with the maximum frequency after replacing every occurrence of X as per the following conditions: If the character present adjacently to the left of X is 1, replace X with 1.If the character prese
15+ min read
Minimum non-adjacent pair flips required to remove all 0s from a Binary String
The problem statement asks us to find the minimum number of operations required to remove all 0s from a given binary string S. An operation involves flipping at most two non-adjacent characters (i.e., characters that are not next to each other) in the string. Let's take the first example given: S =
11 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
Minimize deletions in a Binary String to remove all subsequences of the form "0101"
Given a binary string S of length N, the task is to find the minimum number of characters required to be deleted from the string such that no subsequence of the form â0101â exists in the string. Examples: Input: S = "0101101"Output: 2Explanation: Removing S[1] and S[5] modifies the string to 00111.
6 min read
Check if substring "10" occurs in the given binary string in all possible replacements of '?' with 1 or 0
Given a string S consisting of only '0', '1' and '?', the task is to check if there exists a substring "10" in every possible replacement of the character '?' with either 1 or 0. Examples: Input: S = "1?0"Output: YesExplanation:Following are all the possible replacements of '?': Replacing the '?' wi
7 min read
Minimize removal of substring of 0s to remove all occurrences of 0s from a circular Binary String
Given circular binary string S of size N, the task is to count the minimum number of consecutive 0s required to be removed such that the string contains only 1s. A circular string is a string whose first and last characters are considered to be adjacent to each other. Examples: Input: S = "11010001"
6 min read