1
class Solution {
2
public:
3
string makeLargestSpecial(string s) {
4
if (s.length() == 0) return ""; // return null string if size is zero
5

6
vector<string> ans; // list to store all current special substrings
7
int count = 0,
8
i = 0; // keep track of special substring starting index using "i" and
9
//"count" to keep the track of special substring is over or not
10

11
for (int j = 0; j < s.size(); j++) {
12
if (s[j] == '1')
13
count++;
14
else
15
count--;
16

17
if (count == 0) {
18
// call recursively using mid special substring
19

20
ans.push_back('1' + makeLargestSpecial(s.substr(i + 1, j - i - 1)) + '0');
21
i = j + 1;
22
}
23
}
24
// sort current substring stored list to fulfill the question demand
25

26
sort(ans.begin(), ans.end(), greater<string>());
27
string finalString = "";
28
for (i = 0; i < ans.size(); i++) {
29
finalString += ans[i];
30
}
31
return finalString;
32
}
33
};

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0