296. Special Binary String

HardStringString

A special binary string is a binary string where the number of 0s equals the number of 1s and every prefix has at least as many 1s as 0s. You may repeatedly swap two adjacent special substrings. Given such a string s, return the lexicographically largest string obtainable.

Input: A quoted special binary string.

Output: Quoted string — the largest reachable string.

Examples

Example 1
Input: "11011000"
Output: "11100100"
Explanation: Swap special substrings to maximize.
Example 2
Input: "10"
Output: "10"
Explanation: Single unit.
Example 3
Input: "1100"
Output: "1100"
Explanation: Already maximal.

Constraints

Asked by

AdobeMicrosoftAmazonGoogleBloomberg
Solve this problem in the editor →