原题:
Given two integers A and B, return any string S such that:
Shas lengthA + Band contains exactlyA'a'letters, and exactlyB'b'letters;- The substring
'aaa'does not occur inS; - The substring
'bbb'does not occur inS.
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"
Note:
0 <= A <= 1000 <= B <= 100- It is guaranteed such an
Sexists for the givenAandB.
思路:
贪心算法!!!!
c++代码实现:
class Solution { public: string strWithout3a3b(int A, int B) { string ans=""; char a='a',b='b'; if(A<B){ swap(A,B); swap(a,b); } while(A||B){ if(A>0) {ans+= a; A--;} if(A>B) {ans+= a; A--;} if(B>0) {ans+= b; B--;} } return ans; } };