Write a function solution that, given two integers A and B, returns a string containing exactly A letters 'a' and exactly B letters 'b' with no three consecutive letters being the same (in other words, neither "aaa" nor "bbb" may occur in the returned string).
Examples:
1. Given A = 5 and B = 3, your function may return "aabaabab". Note that "abaabbaa" would also be a correct answer. Your function may return any correct answer.
2. Given A = 3 and B = 3, your function should return "ababab", "aababb", "abaabb" or any of several other strings.
3. Given A = 1 and B = 4, your function should return "bbabb", which is the only correct answer in this case.
Assume that:
- A and B are integers within the range [0..100];
- at least one solution exists for the given A and B.
In your solution, focus on correctness. The performance of your solution will not be the focus of the assessment.
// you can use includes, for example:
// #include <algorithm>
// you can write to stdout for debugging purposes, e.g.
// cout << "this is a debug message" << endl;
string solution(int A, int B) {
string result;
int count_a = 2;
int count_b = 0;
if (B > A) {
count_a = 0;
count_b = 2;
}
while (A > 0 || B > 0) {
if (A > 0 && (A >= B || count_b == 0)) {
result.append("a");
A--;
count_a--;
count_b = 2;
}
if (B > 0 && (B >= A || count_a == 0)) {
result.append("b");
B--;
count_b--;
count_a = 2;
}
}
return result;
}
// you can use includes, for example:
// #include <algorithm>
// you can write to stdout for debugging purposes, e.g.
// cout << "this is a debug message" << endl;
string solution(int A, int B) {
string result;
int count_a = 2;
int count_b = 0;
if (B > A) {
count_a = 0;
count_b = 2;
}
while (A > 0 || B > 0) {
if (A > 0 && (A >= B || count_b == 0)) {
result.append("a");
A--;
count_a--;
count_b = 2;
}
if (B > 0 && (B >= A || count_a == 0)) {
result.append("b");
B--;
count_b--;
count_a = 2;
}
}
return result;
}
// you can use includes, for example:
// #include <algorithm>
// you can write to stdout for debugging purposes, e.g.
// cout << "this is a debug message" << endl;
string solution(int A, int B) {
string result;
int count_a = 2;
int count_b = 0;
if (B > A) {
count_a = 0;
count_b = 2;
}
while (A > 0 || B > 0) {
if (A > 0 && (A >= B || count_b == 0)) {
result.append("a");
A--;
count_a--;
count_b = 2;
}
if (B > 0 && (B >= A || count_a == 0)) {
result.append("b");
B--;
count_b--;
count_a = 2;
}
}
return result;
}
The solution obtained perfect score.