Javascript | 3 Line Simple Solution w/ Explanation | beats 90% / 100%
Idea:\n\nThis is actually a very simple problem to solve, given a few key pieces of understanding. First, any leading 1s in our binary string (B) are already in
***Idea:***\n\nThis is actually a very simple problem to solve, given a few key pieces of understanding. First, any leading **1**s in our binary string (**B**) are already in their ideal position. Second, the conversion from **10** to **01** effectively lets us "bubble" up the **0**s in **B** (ie, **0110 -> 0101 -> 0011**). Third, any series of consecutive **0**s can be converted via **00** to **10** in sequence into a series of **1**s with just a single **0** at the end (ie, **0000 -> 1000 -> 1100 -> 1110**).\n\nKnowing this, we can quickly tailor an ideal solution by bubbling any trailing **0**s up to the very first **0**, and then converting all but the last to a **1**.\n```\n              1 1 1 0 1 0 0 1 1 1 1 0 1 1 0 1 0 1 1 0          // Starting binary string\n\t\t\t        0 <-0 0 <-------0 <---0 <-0 <---0          // Bubble up all 0s to the 1st via 10 to 01\n        -->   1 1 1 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 1 1          \n\t\t\t\t    1 1 1 1 1 1 0                              // Convert all but last 0 to 1s via 00 to 01\n\t\t-->   1 1 1 1 1 1 1 1 1 0 1 1 1 1 1 1 1 1 1 1          // Final maximum value binary string\n```\nThe best result for the code below is **148ms / 48.8MB**.\n\n` `\n***Implementation:***\n\nTo do this, we first need to iterate through the string and find out two things: how many **0**s are there, and what is the index of the first **0**? It\'s easist to accomplish this by running through the string backwards while incrementing the **0**s counter (**z**), and overwriting each time the index of the found **0** at **f**.\n\nIf there are fewer than two **0**s, then we can **return** the original **B**, otherwise we can easily manufacture and **return** the equivalent maximum string in one step, rather than having to process the steps individually.\n\n\n` `\n***Code:***\n```\nvar maximumBinaryString = function(B) {\n    let z = 0, f\n    for (let i = B.length; ~i; i--) if (B[i] === "0") z++, f = i\n    return z < 2 ? B : "1".repeat(f+z-1) + "0" + "1".repeat(B.length-z-f)\n};\n```
['JavaScript']