You are given a binary string binary consisting of only 0's or 1's. You can apply each of the following operations any number of times:


	Operation 1: If the number contains the substring "00", you can replace it with "10".

	
		For example, "00010" -&gt; "10010"
	
	
	Operation 2: If the number contains the substring "10", you can replace it with "01".
	
		For example, "00010" -&gt; "00001"
	
	


Return the maximum binary string you can obtain after any number of operations. Binary string x is greater than binary string y if x's decimal representation is greater than y's decimal representation.

&nbsp;
Example 1:

Input: binary = "000110"
Output: "111011"
Explanation: A valid transformation sequence can be:
"000110" -&gt; "000101" 
"000101" -&gt; "100101" 
"100101" -&gt; "110101" 
"110101" -&gt; "110011" 
"110011" -&gt; "111011"


Example 2:

Input: binary = "01"
Output: "01"
Explanation:&nbsp;"01" cannot be transformed any further.


&nbsp;
Constraints:


	1 &lt;= binary.length &lt;= 105
	binary consist of '0' and '1'.


class Solution:
    def maximumBinaryString(self, binary: str) -> str:
        zero = binary.count('0') # count number of '0'
        zero_idx = binary.index('0') if zero > 0 else 0 # find the index of fist '0' if exists
        one = len(binary) - zero_idx - zero # count number of '1' (not including leading '1's)
        return f"{binary[:zero_idx]}{'1'*(zero-1)}{'0'*min(zero, 1)}{'1'*one}"
class Solution {
    public String maximumBinaryString(String binary) {
        int n = binary.length();
        StringBuffer ans = new StringBuffer("");
        StringBuffer buffer = new StringBuffer("");
        int onesAfter1stZero = 0;
        boolean found1stZero = false;
        for(int i=0;i<n;i++){
            if(found1stZero){
                if(binary.charAt(i)=='0')
                    ans.append('1');
                else
                    buffer.append('1');
            }
            else{
                if(binary.charAt(i)=='0'){
                    found1stZero = true;
                    buffer.append('0');
                }
                else
                    ans.append('1');
            }
        }
        
        ans.append(buffer);
        return ans.toString();
    }
}
class Solution {
public:
    string maximumBinaryString(string binary) {
        int n = binary.size();
        int z_count=0;  //count the number of zeros
        int z_index=-1; //index of the leftmost zero that is why we start iterating from the right
        for(int i=n-1;i>=0;i--) {
         if(binary[i]=='0') {
             z_count++;
             z_index = i;
             binary[i] = '1'; //changing all occurances to 1
         }
        }
        if(z_index!=-1)  //to check if there is atleast 1 zero
        {
            binary[z_index+z_count-1] = '0'; //this is the only zero present
            // -1 because the only zero is also included in zero count 
        }
        return binary;
    }
};
/**
 * @param {string} binary
 * @return {string}
 */
var maximumBinaryString = function(binary) {
    /*
    10 -> 01 allows us to move any zero to left by one position
    00 -> 10 allows us to convert any consicutive 00 to 10
    So we can collect all the zeros together then convert them in 1 except for the rightmost 0
    We will club all the zeros togegher on the rightmost possition, to achieve the biggest value, then covert them into 1 except for the rightmost 0
    So we need to choose indexOfFirstZero as the starting possition of the group of zeros
    If there is no 0 then given string is the maximum possible string
    If there are 1 or more zeros
        Then there will be only 1 zero in the answer
        Position of this will be indexOfFirstZero in given string + countOfZeros - 1
    */
    let firstZeroIndex=-1,zeroCount=0,ans="";
    for(let i=0;i<binary.length;i++){
        if(binary[i]==='0'){
            zeroCount++;
            if(firstZeroIndex===-1){
                firstZeroIndex=i;
            }
        }
    }
    if(firstZeroIndex==-1){
        return binary;
    }
    let onlyZeroInAns = firstZeroIndex + (zeroCount-1);
    for(let i=0;i<binary.length;i++){
        if(i==onlyZeroInAns){
            ans+="0";
        }
        else{
            ans+="1";
        }
    }
    return ans;
};
Maximum Binary String After Change