> For the complete documentation index, see [llms.txt](https://wentao-shao.gitbook.io/leetcode/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://wentao-shao.gitbook.io/leetcode/graph-search/1087.brace-expansion.md).

# 1087.Brace-Expansion

## 1087. Brace Expansion

## 题目地址

<https://leetcode.com/problems/brace-expansion/>

## 题目描述

```
A string S represents a list of words.

Each letter in the word has 1 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, "{a,b,c}d{e,f}" represents the list ["ade", "adf", "bde", "bdf", "cde", "cdf"].

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

Example 1:
Input: "{a,b}c{d,e}f"
Output: ["acdf","acef","bcdf","bcef"]

Example 2:
Input: "abcd"
Output: ["abcd"]

Note:
1 <= S.length <= 50
There are no nested curly brackets.
All characters inside a pair of consecutive opening and ending curly brackets are different.
```

## 代码

### Approach #1 DFS

Time: O(N) && Space: O(N)

```java
class Solution {
  public String[] expand(String S) {
        TreeSet<String> set = new TreeSet<>();
    if (S.length() == 0) {
      return new String[]{""};
    } else if (S.length() == 1) {
      return new String[]{S};
    }

    if (S.charAt(0) == '{') {
      int i = 0;
      while (S.charAt(i) != '}') {
        i++;
      }
      String sub = S.substring(1, i);
      String[] subs = sub.split(",");
      String[] strs = expand(S.substring(i + 1)); // dfs
      for (int j = 0; j < subs.length; j++) {
        for (String str: strs) {
          set.add(subs[j] + str);
        }
      }
    } else {
      String[] strs = expand(S.substring(1));
      for (String str: strs) {
        set.add(S.charAt(0) + str);
      }
    }
    return set.toArray(new String[0]);
  }
}
```

### #2 Without TreeSet

```java
class Solution {
  public String[] expand(String S) {
    int n = S.length();
    if (n == 0) {
      return new String[]{""};
    }
    if (n == 1) {
      return new String[]{S};
    }

    List<String> res = new ArrayList<>();
    if (S.charAt(0) == '{') {
      int count = 0;
      int i = 0;
      while (i < S.length()) {
        if (S.charAt(i) == '}') {
          break;
        }
        i++;
      }
      String[] l = S.substring(1, i).split(",");
      String[] r = expand(S.substring(i + 1));
      for (String ll: l) {
        for (String rr: r) {
          res.add(ll + rr);
        }
      }
    } else {
      String[] r = expand(S.substring(1));
      for (String rr: r) {
        res.add(S.charAt(0) + rr);
      }
    }
    Collections.sort(res);
    return res.toArray(new String[0]);
  }
}
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://wentao-shao.gitbook.io/leetcode/graph-search/1087.brace-expansion.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
