> ## 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.

# Brace Expansion Python Solution with Tests

> Tested Python solution for LeetCode 1087 with 12 pytest cases. Generate a practice environment with lcpy.

LeetCode 1087, [Medium](/catalog/medium). Topics: [String](/catalog/topics/string), [Backtracking](/catalog/topics/backtracking), [Breadth-First Search](/catalog/topics/breadth-first-search). [View on LeetCode](https://leetcode.com/problems/brace-expansion/description/).

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

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

## Problem

You are given a string `s` representing a list of words. Each letter in the word has one or more options.

* If there is one option, the letter is represented as is.
* If there is more than one option, then curly braces delimit the options. For example, `"{a,b,c}"` represents options `["a", "b", "c"]`.

For example, if `s = "a{b,c}"`, the first character is always `'a'`, but the second character can be `'b'` or `'c'`. The original list is `["ab", "ac"]`.

Return all words that can be formed in this manner, sorted in **lexicographical order**.

### Examples

```
Input: s = "{a,b}c{d,e}f"
Output: ["acdf","acef","bcdf","bcef"]
```

```
Input: s = "abcd"
Output: ["abcd"]
```

### Constraints

* 1 \<= s.length \<= 50
* s consists of curly brackets '\{}', commas ',', and lowercase English letters.
* s is guaranteed to be a valid input.
* There are no nested curly brackets.
* All characters inside a pair of consecutive opening and ending curly brackets are different.

## Solution

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

```python theme={"theme":{"light":"github-light","dark":"github-dark"}}
from itertools import product


class Solution:
    # Time: O(n * k) expansions where k is average option count
    # Space: O(result)
    def expand(self, s: str) -> list[str]:
        blocks: list[list[str]] = []
        i = 0
        while i < len(s):
            if s[i] == "{":
                j = s.index("}", i)
                blocks.append(sorted(s[i + 1 : j].split(",")))
                i = j + 1
            else:
                j = s.find("{", i)
                if j == -1:
                    blocks.append([s[i:]])
                    i = len(s)
                else:
                    blocks.append([s[i:j]])
                    i = j
        return sorted("".join(word) for word in product(*blocks))
```

## Complexity

| Time | Space |
| - | - |
| O(n \* k) expansions where k is average option count | O(result) |

## Tags

[NeetCode All](/catalog/neetcode).


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