-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstring-without-aaa-or-bbb.cpp
More file actions
34 lines (34 loc) · 929 Bytes
/
string-without-aaa-or-bbb.cpp
File metadata and controls
34 lines (34 loc) · 929 Bytes
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
class Solution {
public:
// ????????????make sense:https://leetcode.com/problems/string-without-aaa-or-bbb/discuss/226649/JavaC++-(and-Python)-simple-greedy
string strWithout3a3b(int A, int B) {
string res="";
int sum=A+B;
bool flag=true; // flag??????????A?????B?????A??????B???
if(A<B) flag=false;
while(res.size()<sum){
if(flag){
if(A>B && A>=2){
res+="aa";
A-=2;
}
else if(A>=1){
res+="a";
A-=1;
}
}
else{
if(B>A && B>=2){
res+="bb";
B-=2;
}
else if(B>=1){
res+="b";
B-=1;
}
}
flag=!flag;
}
return res;
}
};