forked from AnasImloul/Leetcode-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSpecial Binary String.cpp
46 lines (45 loc) · 1.22 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
37
38
39
40
41
42
43
44
45
46
class Solution {
public:
unordered_map<string,string> dp;
bool special(string s){
int one=0,zero=0;
for(auto ch:s){
if(ch=='1') one++;
else zero++;
if(one<zero) return 0;
}
if(one==zero)
return 1;
return 0;
}
string fun(string s){
if(s.length()==1) return s;
if(dp.count(s)) return dp[s];
string ans=s;
string start;
for(int i=0;i<s.length()-1;i++){
start.push_back(s[i]);
string end=s.substr(i+1);
string comb=end+start;
if(comb>ans and special(start) and special(end)) ans=comb;
}
return dp[s]=ans;
}
string makeLargestSpecial(string s) {
string start;
string ans=s;
string ss=s;
for(int i=0;i<s.length();i++){
string middle;
for(int j=i;j<s.length();j++){
middle.push_back(s[j]);
string end=s.substr(j+1);
string comb=start+fun(middle)+end;
if(comb>ans) ans=comb;
}
start.push_back(s[i]);
}
if(ss==ans) return ans;
return(makeLargestSpecial(ans));
}
};