#761

Special Binary String

specialist · 905 · lc hard +31 · verified · 79.3% accepted · 1,150 likes · top 90%

Description

A special binary string has equal numbers of 0s and 1s, and every prefix contains at least as many 1s as 0s. You are given such a string s.

In one move you may swap any two adjacent non-empty special substrings. Apply any number of such swaps to produce the lexicographically largest possible string, and return it.

Example 1:

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.

Example 2:

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

Code

1
2
3