Watch 7 video solutions for Maximum Binary String After Change, a medium level problem involving String, Greedy. This walkthrough by code Explainer has 3,409 views views. Want to try solving it yourself? Practice on FleetCode or read the detailed text solution.
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:
"00", you can replace it with "10".
"00010" -> "10010""10", you can replace it with "01".
"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.
Example 1:
Input: binary = "000110" Output: "111011" Explanation: A valid transformation sequence can be: "000110" -> "000101" "000101" -> "100101" "100101" -> "110101" "110101" -> "110011" "110011" -> "111011"
Example 2:
Input: binary = "01" Output: "01" Explanation: "01" cannot be transformed any further.
Constraints:
1 <= binary.length <= 105binary consist of '0' and '1'.Problem Overview: You are given a binary string and two allowed operations that move or merge zeros: "00" → "10" and "10" → "01". Apply any number of operations to produce the lexicographically largest possible binary string.
Approach 1: Direct Simulation (O(n^2) time, O(n) space)
This method simulates the allowed operations exactly as defined. You scan the string looking for patterns "00" or "10" and replace them accordingly. Each operation gradually pushes zeros to the right while converting earlier positions into '1'. Because every modification may require rescanning or shifting characters, the process can degrade to quadratic time in the worst case. This approach helps build intuition about how zeros migrate through the string and why eventually almost all characters become '1'.
Approach 2: Two-Pointer Greedy Insight (O(n) time, O(1) space)
The operations effectively guarantee that all zeros collapse into a single position. After repeatedly applying the rules, the final string becomes all '1' except for one '0'. The position of that zero depends on two values: the index of the first zero and the total number of zeros. If the first zero appears at index i and there are k zeros, the final zero ends up at position i + k - 1. You count zeros with a single pass and rebuild the result string with '1' everywhere except that index. A two-pointer or linear scan works well here since you only track the first zero and total zero count. This greedy observation removes the need to simulate operations and gives a clean linear solution.
The reasoning relies on how the operations always move zeros rightward while turning earlier digits into ones. Eventually every zero except one can be absorbed by earlier transformations.
Recommended for interviews: The greedy counting approach using a single pass is what interviewers expect. It demonstrates that you recognized the invariant created by the operations instead of simulating them. Implementing the direct simulation first can show understanding, but the optimal solution using a linear scan and counting zeros shows stronger algorithmic insight with concepts from greedy, string manipulation, and two pointers.
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Direct Simulation | O(n^2) | O(n) | Useful for understanding how the operations transform the string step‑by‑step. |
| Greedy with Two-Pointer Insight | O(n) | O(1) | Best choice for interviews and production solutions when scanning the string once. |