Special binary strings are binary strings with the following two properties:
The number of 0’s is equal to the number of 1’s.
Every prefix of the binary string has at least as many 1’s as 0’s.
Given a special string S, a move consists of choosing two consecutive, non-empty, special substrings of S, and swapping them. (Two strings are consecutive if the last character of the first string is exactly one index before the first character of the second string.)
At the end of any number of moves, what is the lexicographically largest resulting string possible?
Example 1:
1 2 3 4 5
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.
Note:
S has length at most 50.
S is guaranteed to be a special binary string as defined above.
classSolution { public: stringmakeLargestSpecial(string S){ int len = S.length(); string ans = S; do { S = ans; for (int i = 0; i < len; i++) { for (int j = 1; j + i <= len; j++) { string tmp = S.substr(i, j); if (!isSpecial(tmp)) continue;
int cnt = 0; string maxStr = tmp; for (int k = 0; k < tmp.length(); k++) { if (tmp[k] == '0') cnt--; else cnt++;
if (k != tmp.length() - 1 && cnt == 0) { string a = tmp.substr(0, k + 1), b = tmp.substr(k + 1); maxStr = max(maxStr, max(a + b, b + a)); } }
ans = max(ans, S.substr(0, i).append(maxStr).append(S.substr(i + j))); } } } while (ans != S);
return ans; }
boolisSpecial(conststring& str){ int cnt = 0; for (int i = 0; i < str.length(); i++) { if (str[i] == '0') cnt--; else cnt++; if (cnt < 0) returnfalse; } return cnt == 0; } };