In the coding school, the teacher gave his students a binary string ‘bString’, and asked them to make the greatest binary string possible out of it by applying the below-mentioned operations any number of times. The two allowed operations are:
Operation 1: If the string contains the substring "00", the student can replace it with "10". For example, "00010" -> "10010"
Operation 2: If the string contains the substring "10", the student can replace it with "01". For example, "00010" -> "00001"
Assuming that you are the student, find the greatest binary string one can obtain after applying the above operations any number of times.
A Binary string ‘X’ is said to be greater than binary string ‘Y’, if X's decimal representation is greater than Y's decimal representation.
Example:
Let’s say we have bstring as “001” which is an integer 1 in decimal representation. If you apply the above-given operation then you will have bString as “101” which is an integer 5 in decimal representation.
The first line of input contains an integer ‘T’, denoting the number of test cases.
The first line of each test case contains a binary string ‘bString’ of 0’s and 1’s.
Output format:
For each test case, print the greatest binary string you can obtain after applying the given operations.
Output for each test case is printed on a separate line.
Note:
You do not need to print anything. It has already been taken care of. Just implement the given function.
1 <= T <= 10
1 <= N <= 10^5
The binary string will only consist of 0’s and 1’s as characters.
Where ‘T’ represents the number of test cases, ‘N’ represents the length of the binary string.
Time Limit: 1 sec
2
000110
01
111011
01
For the first test case,
Given the binary string as “000110”. So we will first apply operation 2 on “000110” and the string will be converted to “000101”.Then again we will apply operation 2 on “000101” and the string will be converted to “000011”. Now will apply operation 1 on “000011” and the string will be converted to “100011”. Then again apply operation 1 on “100011” and it will be converted to “110011”. Now again we will apply operation 1 on “110011” and it will be converted to “111011”. Now we can’t apply any further given operations on the string “111011” so our final output is “111011”.
For the second test case,
Given the binary string as “01”. Since from the given operations available none of them is applicable so our final output will be “01” itself.
2
1111
000010
1111
111101
The greatest binary string will have at most one ‘0’ in the string after applying the operation.
Algorithm:
O(N), where ‘N’ is the length of the binary string.
Since we are traversing the string two times, first to find the location of the first zero in the string, and then to count the total number of zeros in the string and also to fill the string with ones side by side. Each of the two traversals takes O(N) time. So overall, time complexity will be O(N).
O(1),
Since constant space is used. So overall space complexity will be O(1).