forked from AnasImloul/Leetcode-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathString Without AAA or BBB.cpp
70 lines (62 loc) · 1.94 KB
/
String Without AAA or BBB.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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
class Solution {
public:
string strWithout3a3b(int a, int b) {
string ans="";
int i=0,j=0;
bool flag = false;
if(a > b){
for(j=0;j<b;j++,i+=2){
if(a-i == b-j){ // If this condition satisfies means from this point our count of remaining a and b become same so we need to print them alternatively.
flag = true;
break;
}
ans.push_back('a');
ans.push_back('a');
ans.push_back('b');
}
if(flag == true){
//Printing a and b alternately as count of remaining a and b is same.
for(i;i<a;i++){
ans.push_back('a');
ans.push_back('b');
}
}
else{ // If count does not become same then we print 2 a and 1 b in upper for loop and still some a remains we print them in below loop.
while(i<a){
ans.push_back('a');
i++;
}
}
}
else if( b> a){
for(i=0;i<a;i++,j+=2){
if(a-i == b-j){
flag = true;
break;
}
ans.push_back('b');
ans.push_back('b');
ans.push_back('a');
}
if(flag == true){
for(i;i<a;i++){
ans.push_back('a');
ans.push_back('b');
}
}
else{
while(j<b){
ans.push_back('b');
j++;
}
}
}
else{
for(i=0;i<a;i++){
ans.push_back('a');
ans.push_back('b');
}
}
return ans;
}
};