---
# System prepended metadata

title: 0984. String Without AAA or BBB
tags: [Leetcode, Task Schedule, Priority Queue, Medium, Greedy]

---

# 0984. String Without AAA or BBB
###### tags: `Leetcode` `Medium` `Greedy` `Task Schedule` `Priority Queue`
Link:https://leetcode.com/problems/string-without-aaa-or-bbb/
## 思路 $O(N)$ $O(1)$
先找到哪个字母个数比较多，就先排哪个
例如如果a比较多，那么每次就先放a，如果放完了之后a还是比b多就再放一个a，然后如果还有b就再放b，就可以保证前面是aab后面是ab，而且不会有单独连着好几个a出现
## Code
```java=
class Solution {
    public String strWithout3a3b(int a, int b) {
        char more, less;
        
        if(a>b){
            more = 'a';
            less = 'b';
        }
        else{
            more = 'b';
            less = 'a';
            int temp = b;
            b = a;
            a = temp;
        }
        StringBuilder sb = new StringBuilder(a+b);
        while(a>0){
            sb.append(more);
            a--;
            if(a>b){
                sb.append(more);
                a--;
            }
            if(b>0){
                sb.append(less);
                b--;
            }
        }
        return sb.toString();
    }
}
```