Untitled
Anonymous
c_cpp
10/10/2024 1:42 PM
660 B
17
Indexable
string largestMagical(string binString) {
if (binString.empty()) return binString;
vector<string> ans;
int cnt = 0, j = 0;
for (int i = 0; i < binString.size(); ++i) {
cnt += binString[i] == '1' ? 1 : -1;
if (cnt == 0) {
ans.push_back("1" + largestMagical(binString.substr(j + 1, i - j - 1)) + "0");
j = i + 1;
}
}
sort(ans.begin(), ans.end(), greater<string>());
return accumulate(ans.begin(), ans.end(), string{});
} Editor is loading...
Leave a Comment