# 984. String Without AAA or BBB

**<font color=red>难度: Easy</font>**

## 刷题内容

> 原题连接

* https://leetcode.com/problems/string-without-aaa-or-bbb/

> 内容描述

```
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"
 

Note:

0 <= A <= 100
0 <= B <= 100
It is guaranteed such an S exists for the given A and B.
```

## 解题方案

> 思路 1
******- 时间复杂度: O(A+B)******- 空间复杂度: O(1)******


自己构造一下，首先我们不能有连续3个的a和b出现，那我们就想到说2个2个的出现，然后这样可能会出现一种情况，就是A搞完了B还剩下超过3个，那就不行了


所以想到比较一下A和B的大小，如果A更大，我就每次2个a和1个b，直到A不比B大了就开始'ab'模式了


```python
class Solution:
    def strWithout3a3b(self, A, B):
        """
        :type A: int
        :type B: int
        :rtype: str
        """
        if A > B:
            res = ''
            while A > B and B > 0 and A > 0:
                res += 'aab'
                A, B = A-2, B-1
            res += 'ab' * min(A, B)
            if A > B:
                res += 'a' * A
            elif B > A:
                res += 'b' * B
        elif B > A:
            res = ''
            while B > A and A > 0 and B > 0:
                res += 'bba'
                A, B = A-1, B-2
            res += 'ba' * min(A, B)
            if A > B:
                res += 'a' * A
            elif B > A:
                res += 'b' * B
        else:
            return 'ab' * A
        return res
```





简化一下代码


```python
class Solution:
    def strWithout3a3b(self, A, B):
        """
        :type A: int
        :type B: int
        :rtype: str
        """
        if A > B:
            res = ''
            while A > B and A > 0 and B > 0:
                res += 'aab'
                A, B = A-2, B-1
            res += 'ab' * min(A, B)
            if A > B:
                res += 'a' * A
            elif B > A:
                res += 'b' * B
        elif B > A:
            res = ''
            for c in self.strWithout3a3b(B, A):
                if c == 'a':
                    res += 'b'
                else:
                    res += 'a'
        else:
            return 'ab' * A
        return res
```










































