Given two integers A and B, return any string S such that:
S has length A + B and contains exactly A ‘a’ letters, and exactly B ‘b’ letters;
The substring ‘aaa’ does not occur in S;
The substring ‘bbb’ does not occur in S.
Example 1:
Input: A = 1, B = 2
Output: "abb"
Explanation: "abb", "bab" and "bba" are all correct answers.
Example 2:
Input: A = 4, B = 1
Output: "aabaa"
思路:显然题目让我们需要输出A个a,B个b,但是不能出现连续的3个a,3个b。
那么我们采用贪心的方法,当A多时,每次输出2个a,1个b,。当B多时,每次输出2两个b,一个a。相等时,输出1个a,1个b。
string strWithout3a3b(int A, int B) {
if(A==0)return string(B,'b');
else if(B==0)return string(A,'a');
else if(A==B)return "ab"+strWithout3a3b(A-1,B-1);
else if(A>B)return "aab"+strWithout3a3b(A-2,B-1);
else
return strWithout3a3b(A-1,B-2) +"abb";
}