-
Notifications
You must be signed in to change notification settings - Fork 119
/
Copy pathSpecial Binary String.cpp
36 lines (29 loc) · 1.1 KB
/
Special Binary String.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
// Runtime: 0 ms (Top 100.0%) | Memory: 6.80 MB (Top 47.46%)
class Solution {
public:
string makeLargestSpecial(string s) {
if(s.length()==0)
return ""; //return null string if size is zero
vector<string> ans; //list to store all current special substrings
int count=0,i=0; //keep track of special substring starting index using "i" and
//"count" to keep the track of special substring is over or not
for(int j=0;j<s.size();j++){
if(s[j] == '1')
count++;
else
count--;
if(count==0){
//call recursively using mid special substring
ans.push_back('1' + makeLargestSpecial(s.substr(i+1,j-i-1)) + '0');
i = j+1;
}
}
//sort current substring stored list to fulfill the question demand
sort(ans.begin(),ans.end(),greater<string>());
string finalString = "";
for(i=0;i<ans.size();i++){
finalString += ans[i];
}
return finalString;
}
};