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.
Input: str = “101100”
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”
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:
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. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.