Maximum Binary String After Change - Problem
You are given a binary string binary consisting of only 0's and 1's. You can apply each of the following operations any number of times:
Operation 1: If the string contains the substring "00", you can replace it with "10".
For example, "00010" → "10010"
Operation 2: If the string contains the substring "10", you can replace it with "01".
For example, "00010" → "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.
Input & Output
Example 1 — Basic Case
$
Input:
binary = "000110"
›
Output:
"111011"
💡 Note:
Apply operations: 000110 → 100110 → 110110 → 111010 → 111001. No more beneficial operations possible.
Example 2 — Single Zero
$
Input:
binary = "01"
›
Output:
"10"
💡 Note:
Direct application of Operation 2: 01 becomes 10 using the 10→01 rule in reverse.
Example 3 — No Zeros
$
Input:
binary = "111"
›
Output:
"111"
💡 Note:
String contains no zeros, so no operations can be applied. Return unchanged.
Constraints
- 1 ≤ binary.length ≤ 105
- binary consists of only '0' and '1'
Visualization
Tap to expand
Understanding the Visualization
1
Input
Binary string "000110" with operations available
2
Operations
00→10 and 10→01 can be applied repeatedly
3
Output
Maximum possible binary string "111011"
Key Takeaway
🎯 Key Insight: Move all zeros right except one, placed at the optimal position for maximum decimal value
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code