Related Articles

# Print string after removing all (“10” or “01”) from the binary string

• Last Updated : 29 Apr, 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:
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:

1. 010110100 -> 0110100
2. 0110100 -> 10100
3. 10100 -> 100
4. 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:

1. Get the count of 0’s present in the string and store it in a variable.
2. Get the count of 1’s present in the string and store it in another variable.
3. If the count of 1’s is equal to 0’s, then the entire string can be reduced. Therefore, return -1.
4. 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.
5. 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 ``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

 ``
Output:
`00000`

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).

Attention reader! Don’t stop learning now. Participate in the Scholorship Test for First-Step-to-DSA Course for Class 9 to 12 students.

My Personal Notes arrow_drop_up