All problems
0945HardStringDivide and ConquerSorting

Largest Rearrangement of a Nested Tape

Tracked in this browser only
Write code

Trains the technique from

LeetCode 761Special Binary String

This is an original problem, written from a brief that listed the technique, the difficulty, the topics, the function shape and the input bounds — none of that problem's wording, examples, hints or editorials. The link is there so you can map your practice onto the standard set.

Same function shape, different story and different numbers.

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 is preloaded with this. It matches the parent problem's shape, so a solution that works here transfers to a judge unchanged.

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