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).
Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.
- Print reverse string after removing vowels
- Removing string that is an anagram of an earlier string
- Periodic Binary String With Minimum Period and a Given Binary String as Subsequence.
- Print the longest prefix of the given string which is also the suffix of the same string
- Print all possible combinations of the string by replacing '$' with any other digit from the string
- String obtained by reversing and complementing a Binary string K times
- Longest sub string of 0's in a binary string which is repeated K times
- Removing punctuations from a given string
- Removing spaces from a string using Stringstream
- Python | Check if a given string is binary string or not
- Largest sub-string of a binary string divisible by 2
- Balance a string after removing extra brackets
- Find the resultant String after replacing X with Y and removing Z
- Modify string by removing vowels in between two consonants
- Ways of transforming one string to other by removing 0 or more characters
- Character replacement after removing duplicates from a string
- Lexicographically smallest string formed by removing at most one character
- Check if string is palindrome after removing all consecutive duplicates
- Reduce the string by removing K consecutive identical characters
- Minimize the length of string by removing occurrence of only one character
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.