C# Greedy Solution
Intuition

Key Insight: The operations allow you to manipulate the positions of 0s and 1s in the string. The two operations ensure that:

Multiple 0s can eventu
# Intuition
- <!-- Describe your first thoughts on how to solve this problem. -->Key Insight: The operations allow you to manipulate the positions of 0s and 1s in the string. The two operations ensure that:
- Multiple 0s can eventually merge into a single 0.
- All other positions can be filled with 1s.
- The final string maximization is achieved by shifting as many 1s to the left as possible, leaving at most one 0 and ensuring it occupies the rightmost feasible position among the original zeros.
- Simplification: After observing the problem constraints, you can deduce that:
- The final string will have all 1s up to a certain position and at most one 0 afterward.
- The position of the remaining 0 is determined by the number of 1s before the first 0 (leadingOnes) and the count of all zeros in the string (zeroCount).
- Final State: The maximum binary string has a single 0 (if any zeros exist) positioned at the furthest right position where merging multiple zeros would lead to the highest decimal representation.
___
# Approach
- <!-- Describe your approach to solving the problem. -->Analyze Leading 1s: Identify the number of 1s at the start of the string since they cannot be impacted by the operations.
- Count Remaining 0s: After the leading 1s, count how many 0s exist in the remainder of the string.
- Construct the Result: All positions up to leadingOnes + zeroCount - 1 should be filled with 1s.
- Place a single 0 at the position leadingOnes + zeroCount - 1.
- Fill the rest of the string (if any) with 1s.
___
# Complexity
- Time complexity:
<!-- Add your time complexity here, e.g. $$O(n)$$ -->O(n), The algorithm processes the string a few times in a linear pass.
___
- Space complexity:
<!-- Add your space complexity here, e.g. $$O(n)$$ -->O(n), as we allocate an array to construct the resulting string.
___
# Code
```csharp []
public class Solution {
    public string MaximumBinaryString(string binary) {
        int n = binary.Length;
        int leadingOnes = 0;

        // Count the number of leading ones
        while (leadingOnes < n && binary[leadingOnes] == '1') {
            leadingOnes++;
        }

        // Count the number of zeros after the leading ones
        int zeroCount = 0;
        for (int i = leadingOnes; i < n; i++) {
            if (binary[i] == '0') {
                zeroCount++;
            }
        }

        // If there are no zeros or only one zero, the string is already maximized
        if (zeroCount <= 1) {
            return binary;
        }

        // The resulting string will have all ones, except one zero at position (leadingOnes + zeroCount - 1)
        char[] result = new char[n];
        for (int i = 0; i < n; i++) {
            result[i] = '1'; // Initialize with all ones
        }

        // Place the single zero
        result[leadingOnes + zeroCount - 1] = '0';

        return new string(result);
    }
}

```
['String', 'Greedy', 'C#']