🧩 Greedy String Builder – No "aaa" or "bbb" Allowed! 🚫🔥
IntuitionTo avoid having three consecutive 'a's or 'b's, we need to balance the frequency of both characters carefully.
If one character appears more than the o
# Intuition
To avoid having three consecutive 'a's or 'b's, we need to balance the frequency of both characters carefully.
If one character appears more than the other, we can occasionally place two of the more frequent character, followed by one of the other, to prevent violating the "no three consecutive letters" rule.



# Approach
- Use a StringBuilder to construct the result.

- While either a or b is still greater than zero:

    - If a > b, we prefer placing "aa" followed by "b" (if b is still available).

    - If b > a, we prefer placing "bb" followed by "a" (if a is still available).

    - If a == b, we alternate between 'a' and 'b' (since both are balanced).

- This greedy approach makes sure we never add three of the same letter consecutively.

# Complexity
- **Time complexity:**
O(a+b)
We are building a string of length a + b, and each character is appended once.

- **Space complexity:**
O(a+b)
The space is used to store the final result string.



# Code
```java []
class Solution {
    public String strWithout3a3b(int a, int b) {
        StringBuilder sb = new StringBuilder();

        while (a > 0 || b > 0) {
            // Choose whether to append 'a' or 'b'
            if (a > b) {
                if (a >= 2) {
                    sb.append("aa");
                    a -= 2;
                } else {
                    sb.append("a");
                    a--;
                }
                if (b > 0) {
                    sb.append("b");
                    b--;
                }
            } else if (b > a) {
                if (b >= 2) {
                    sb.append("bb");
                    b -= 2;
                } else {
                    sb.append("b");
                    b--;
                }
                if (a > 0) {
                    sb.append("a");
                    a--;
                }
            } else {
                // a == b, alternate them
                if (a > 0) {
                    sb.append("a");
                    a--;
                }
                if (b > 0) {
                    sb.append("b");
                    b--;
                }
            }
        }

        return sb.toString();
    }
}

```
['Java']