Show problems as
Classic problem #761HardStringDivide and ConquerSorting

Special Binary String

Real-world version: Largest Rearrangement of a Nested Tape
Tracked in this browser only
Read Special Binary String on LeetCode(opens in a new tab)

The statement here is our own: the same inputs, outputs and technique.

A tape of '0' and '1' is nested when it holds as many of each and, reading from the left, the count of '1' never falls behind the count of '0'.

The tape s is nested. You may repeatedly pick two nested stretches that sit directly next to each other and swap them, as often as you like.

Return the largest tape obtainable, comparing tapes as text.

Examples

Example 1

Input
s = "110100111000"
Output
"111000110100"

The tape breaks into the two pieces 110100 and 111000. Neither has anything worth reordering inside it, and as text 111000 is the larger, so it goes first.

Example 2

Input
s = "1010"
Output
"1010"

Two identical pieces sit side by side, so no swap changes anything.

Example 3

Input
s = "101100"
Output
"110010"

The pieces are 10 and 1100, and putting the larger first gives 110010.

Constraints

  • 1 <= s.length <= 50
  • s[i] is either '0' or '1'
  • s is nested

The signature

The editor starts with this. It matches the parent problem's shape, so a solution that works here works on a standard judge unchanged.

Python
def make_largest_special(s: str) -> str:
Java
public String makeLargestSpecial(String s)