> ## Documentation Index
> Fetch the complete documentation index at: https://leetcode-py.wisl.dev/llms.txt
> Use this file to discover all available pages before exploring further.

> ## Agent Instructions
> leetcode-py is a Python LeetCode practice environment generator with one CLI: lcpy. It is not a service or platform.
> Each problem is a directory under leetcode/ with README.md, solution.py, test_solution.py, helpers.py, and playground.ipynb. lcpy gen creates them from JSON templates bundled with the package.
> Examples are backed by tests; copy them verbatim.

# Special Binary String Python Solution

> Tested Python solution for LeetCode 761 with 19 pytest cases. Generate a practice environment with lcpy.

LeetCode 761, [Hard](/catalog/hard). Topics: [String](/catalog/topics/string), [Divide and Conquer](/catalog/topics/divide-and-conquer), [Sorting](/catalog/topics/sorting). [View on LeetCode](https://leetcode.com/problems/special-binary-string/description/).

Generate this problem as a practice environment: tested reference solution, 19 [parametrized pytest cases](/practice/testing), and a playground notebook:

```bash theme={"theme":{"light":"github-light","dark":"github-dark"}}
lcpy gen -n 761   # by problem number
lcpy gen -s special_binary_string   # by problem name
```

## Problem

Special binary strings are binary strings with the following two properties:

* The number of 0's is equal to the number of 1's.
* Every prefix of the binary string has at least as many 1's as 0's.

You are given a special binary string `s`.

A move consists of choosing two consecutive, non-empty, special substrings of `s`, and swapping them. Two strings are consecutive if the last character of the first string is exactly one index before the first character of the second string.

Return the lexicographically largest resulting string possible after applying the mentioned operations on the string.

### Examples

```
Input: s = "11011000"
Output: "11100100"
Explanation: The strings "10" [occuring at s[1]] and "1100" [at s[3]] are swapped.
This is the lexicographically largest string possible after some number of swaps.
```

```
Input: s = "10"
Output: "10"
```

### Constraints

* 1 \<= s.length \<= 50
* s\[i] is either '0' or '1'.
* s is a special binary string.

## Solution

Reference implementation from [solution.py on GitHub](https://github.com/wislertt/leetcode-py/blob/main/leetcode/special_binary_string/solution.py), full suite in [test\_solution.py](https://github.com/wislertt/leetcode-py/blob/main/leetcode/special_binary_string/test_solution.py):

```python theme={"theme":{"light":"github-light","dark":"github-dark"}}
class Solution:
    # Time: O(n^2) amortized over recursion (sorting dominates at each level)
    # Space: O(n^2) recursion depth O(n) plus per-level substring copies
    def make_largest_special(self, s: str) -> str:
        bal = 0
        start = 0
        parts: list[str] = []
        for i, ch in enumerate(s):
            bal += 1 if ch == "1" else -1
            if bal == 0:
                parts.append("1" + self.make_largest_special(s[start + 1 : i]) + "0")
                start = i + 1
        parts.sort(reverse=True)
        return "".join(parts)
```

## Complexity

| Time | Space |
| - | - |
| O(n^2) amortized over recursion (sorting dominates at each level) | O(n^2) recursion depth O(n) plus per-level substring copies |

## Tags


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.