> ## Documentation Index
> Fetch the complete documentation index at: https://leetcode-py.wisl.dev/llms.txt
> Use this file to discover all available pages before exploring further.

> ## Agent Instructions
> leetcode-py is a Python LeetCode practice environment generator with one CLI: lcpy. It is not a service or platform.
> Each problem is a directory under leetcode/ with README.md, solution.py, test_solution.py, helpers.py, and playground.ipynb. lcpy gen creates them from JSON templates bundled with the package.
> Examples are backed by tests; copy them verbatim.

# String Without AAA or BBB Python Solution

> Tested Python solution for LeetCode 984 with 16 pytest cases. Generate a practice environment with lcpy.

LeetCode 984, [Medium](/catalog/medium). Topics: [String](/catalog/topics/string), [Greedy](/catalog/topics/greedy). [View on LeetCode](https://leetcode.com/problems/string-without-aaa-or-bbb/description/).

Generate this problem as a practice environment: tested reference solution, 16 [parametrized pytest cases](/practice/testing), and a playground notebook:

```bash theme={"theme":{"light":"github-light","dark":"github-dark"}}
lcpy gen -n 984   # by problem number
lcpy gen -s string_without_aaa_or_bbb   # by problem name
```

## Problem

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`, and
* The substring `'bbb'` does not occur in `s`.

### Examples

```
Input: a = 1, b = 2
Output: "abb"
Explanation: "abb", "bab" and "bba" are all correct answers.
```

```
Input: a = 4, b = 1
Output: "aabaa"
```

### Constraints

* `0 <= a, b <= 100`
* It is guaranteed such an `s` exists for the given `a` and `b`.

## Solution

Reference implementation from [solution.py on GitHub](https://github.com/wislertt/leetcode-py/blob/main/leetcode/string_without_aaa_or_bbb/solution.py), full suite in [test\_solution.py](https://github.com/wislertt/leetcode-py/blob/main/leetcode/string_without_aaa_or_bbb/test_solution.py):

```python theme={"theme":{"light":"github-light","dark":"github-dark"}}
class Solution:
    # Time: O(a + b)
    # Space: O(a + b)
    def str_without3a3b(self, a: int, b: int) -> str:
        result: list[str] = []
        while a > 0 or b > 0:
            # Two identical letters in a row: the other letter is forced.
            # Otherwise extend with the letter that is still more plentiful
            forced = len(result) >= 2 and result[-1] == result[-2]
            write_a = result[-1] == "b" if forced else a >= b
            if write_a:
                result.append("a")
                a -= 1
            else:
                result.append("b")
                b -= 1
        return "".join(result)
```

## Complexity

| Time | Space |
| - | - |
| O(a + b) | O(a + b) |

## Tags


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.