Problem

Special Binary String

FULLTIMEOA

A special binary string is a binary string with both of the following properties:

  • The number of '0' characters is equal to the number of '1' characters.
  • Every prefix of the string has at least as many '1' characters as '0' characters.

You are given a special binary string s.

In one move, choose two consecutive non-empty special substrings of s and swap them. Two substrings are consecutive if the last character of the first substring is immediately before the first character of the second substring.

Return the lexicographically largest string possible after applying any number of these moves.

Examples
01 · Example 1
s = "11011000"
return = "11100100"

The two consecutive special substrings "10" and "1100" inside "11011000" can be swapped to produce "11100100".

02 · Example 2
s = "10"
return = "10"

The string is already the only possible special binary string with these characters.

Constraints
  • s is a special binary string.
  • s contains only '0' and '1'.
drafts saved locally
public String makeLargestSpecial(String s) {
  // write your code here
}
s"11011000"
expected"11100100"
sign in to submit