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++
#include <bits/stdc++.h>
using namespace std;
void finalString(string str)
{
int x = 0, y = 0;
int left;
int n = str.length();
for ( int i = 0; i < n; i++) {
if (str[i] == '1' )
x++;
else
y++;
}
if (x > y)
left = 1;
else
left = 0;
int length = n - 2 * min(x, y);
for ( int i = 0; i < length; i++) {
cout << left;
}
}
int main()
{
string str = "010110100100000" ;
finalString(str);
return 0;
}
|
Java
import java.util.*;
class GFG{
static void finalString(String str)
{
int x = 0 , y = 0 ;
int left;
int n = str.length();
for ( int i = 0 ; i < n; i++) {
if (str.charAt(i) == '1' )
x++;
else
y++;
}
if (x > y)
left = 1 ;
else
left = 0 ;
int length = n - 2 * Math.min(x, y);
for ( int i = 0 ; i < length; i++) {
System.out.print(left);
}
}
public static void main(String[] args)
{
String str = "010110100100000" ;
finalString(str);
}
}
|
Python3
def finalString(st):
x , y = 0 , 0
n = len (st)
for i in range ( n):
if (st[i] = = '1' ):
x + = 1
else :
y + = 1
if (x > y):
left = 1
else :
left = 0
length = n - 2 * min (x, y);
for i in range (length):
print (left, end = "")
if __name__ = = "__main__" :
st = "010110100100000"
finalString(st)
|
C#
using System;
class GFG{
static void finalString(String str)
{
int x = 0, y = 0;
int left;
int n = str.Length;
for ( int i = 0; i < n; i++) {
if (str[i] == '1' )
x++;
else
y++;
}
if (x > y)
left = 1;
else
left = 0;
int length = n - 2 * Math.Min(x, y);
for ( int i = 0; i < length; i++) {
Console.Write(left);
}
}
public static void Main(String[] args)
{
String str = "010110100100000" ;
finalString(str);
}
}
|
Javascript
<script>
function finalString(str)
{
let x = 0, y = 0;
let left;
let n = str.length;
for (let i = 0; i < n; i++) {
if (str[i] == '1' )
x++;
else
y++;
}
if (x > y)
left = 1;
else
left = 0;
let length = n - 2 * Math.min(x, y);
for (let i = 0; i < length; i++) {
document.write(left);
}
}
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).
Share your thoughts in the comments
Please Login to comment...