One pass bubble-up solution
In this problem, it\'\'s obvious that we need to make as many high bits (left to the right) to be \'1\' as posibble, if we want to get the biggest number after 
In this problem, it\'\'s obvious that we need to make as many high bits (left to the right) to be \'**1**\' as posibble, if we want to get the biggest number after operations.\n\nThen let\'s think about the operation we can do, the **1st operation** \'**00 -> 10**\' can let the higher bit become \'**1**\'. Howerver, \'**0**\' bits are not alway become neighbors, ie. how can we make binary string biggest like \'**011111111111111111111110**\'?\n\nNow the second operation works, the **2nd operation** \'**10 -> 01**\' can move lower \'**0**\' bit to the higher bit, so we can move the last \'**0**\' the the second position after serveral **2nd operations**, and \'**011111111111111111111110**\' become \'**001111111111111111111111**\', then we can apply 1st operation to this string to make it bigger:  \'**001111111111111111111111**\' ->  \'**101111111111111111111111**\'\n\nNow we conclude our basic thought: \n+ **1st operation** make two neighbor \'**0**\'s become bigger binary string \'**10**\'.\n+ **2nd operation** can move \'**0**\' and make the first two \'**0**\'s become neighbors.\n\nSo we can move and set \'**0**\' to be \'**1**\' starts from the left side, the whole processing showed in the img below, it was called bubble-up cause the \'**0**\' move operation looks like bubble-up and the \'**0**\' looks like a bubble ...\n![image](https://assets.leetcode.com/users/images/1d45f9f1-f7fe-479e-8d1c-79b5b1e1798c_1610100728.0545764.png)\n\nThis whole processing can be done in one pass: \n```java\n    private static final char BUBBLE = \'0\', WATER = \'1\';\n\n    public String maximumBinaryString(String waterString) {\n        char[] waters = waterString.toCharArray();\n        int curBubble = 0, nxtBubble;\n        \n        // 1. Initially find the first bubble to be the current bubble\n        for (; curBubble < waters.length && waters[curBubble] == WATER; curBubble ++) {}\n        \n        // 2. Initially find the second bubble to be the next bubble\n        for (nxtBubble = curBubble + 1; nxtBubble < waters.length && waters[nxtBubble] == WATER; nxtBubble ++) {}\n        \n        while (nxtBubble < waters.length) {\n            // 3. the next bubble rise to replace the position behind current bubble for operation \'0xxxxxxx0\' -> \'00xxxxxx0\'\n            waters[curBubble + 1] = BUBBLE;\n            \n            // 4. and water will fill the space of next bubble for its bubble up \'00xxxxxx0\' -> \'00xxxxxxx\'\n            if (nxtBubble > curBubble + 1) { waters[nxtBubble] = WATER; }\n            \n            // 5. current bubble burst when next bubble comes to it for operation \'00\' -> \'10\'\n            waters[curBubble] = WATER;\n            \n            // 6. the next bubble become the current bubble after last bubble burst\n            curBubble += 1;\n            \n            // 7. and we continue to find the next bubble from the position of last next bubble\n            for (nxtBubble += 1; nxtBubble < waters.length && waters[nxtBubble] == WATER; nxtBubble ++) {}\n        }\n        return new String(waters);\n    }\n```\n\nThis one pass solution takes O(n) time and O(n) space (to build the result which has same length with binary string).\n\nWe rethink about the whole processing, we found that there alway only one bubble left cause each time we move the 2nd bubble next to the 1st bubble and burst this 1st bubble, then we can also predict the last bubble position is ``` last_bubble_pos = first_bubble_pos + bubble_num - 1 ```, cause each time the 1st bubble position will move 1 pos, and it will move ``` bubble_num - 1 ``` times (except the last bubble). Now we can change our code in this way: \n\n```java\n    public String maximumBinaryString(String binary) {\n        char[] res = new char[binary.length()];\n        Arrays.fill(res, \'1\');\n        int firstZero = 0, zeroCount = 1, pvt;\n\n        // find the first zero\n        for ( ; firstZero < binary.length() && binary.charAt(firstZero) == \'1\'; firstZero ++) {}\n\n        // count zero number\n        for (pvt = firstZero + 1;\n             pvt < binary.length();\n             zeroCount += (binary.charAt(pvt) == \'0\' ? 1 : 0), pvt ++) {}\n\n        // set the last zero position in the new binary string\n        if (firstZero < binary.length()) { res[firstZero + zeroCount - 1] = \'0\'; }\n        return new String(res);\n    }\n```
[]