LeetCode 1087: Brace Expansion Solution

Master LeetCode problem 1087 (Brace Expansion), a medium challenge, with our optimized solutions in Java, C++, and Python. Explore detailed explanations, test your code in our interactive editor, and prepare for coding interviews.

1087. Brace Expansion

Problem Explanation

Explanation:

To solve this problem, we can use a recursive approach. We iterate through the input string and when we encounter a '{', we recursively call a function to process the options within the braces. We keep track of the current output string as we iterate through the input. At the end, we return all possible combinations.

  1. Initialize an empty list to store the result.
  2. Create a recursive function to process the options within braces:
    • If we encounter a letter, we add it to the current output string and recursively call the function on the next character.
    • If we encounter a '{', we find the corresponding '}' and process the options within the braces by recursively calling the function on each option.
  3. After processing the entire input string, we add the current output string to the result list.
  4. Finally, we return the sorted list of all possible combinations.

Time Complexity: O(N * 2^M) where N is the length of the input string and M is the number of options within braces (average number of options).

Space Complexity: O(N) where N is the length of the input string.

:

Solution Code

class Solution {
    public String[] expand(String S) {
        List<String> result = new ArrayList<>();
        backtrack(S, "", result, 0);
        return result.toArray(new String[0]);
    }
    
    private void backtrack(String s, String current, List<String> result, int index) {
        if (index == s.length()) {
            result.add(current);
            return;
        }
        
        if (s.charAt(index) == '{') {
            int end = index + 1;
            while (end < s.length() && s.charAt(end) != '}') {
                end++;
            }
            String[] options = s.substring(index + 1, end).split(",");
            Arrays.sort(options);
            for (String option : options) {
                backtrack(s, current + option, result, end + 1);
            }
        } else {
            backtrack(s, current + s.charAt(index), result, index + 1);
        }
    }
}

Try It Yourself

Loading code editor...

Related LeetCode Problems

Frequently Asked Questions

How to solve LeetCode 1087 (Brace Expansion)?

This page provides optimized solutions for LeetCode problem 1087 (Brace Expansion) in Java, C++, and Python, along with a detailed explanation and an interactive code editor to test your code.

What is the time complexity of LeetCode 1087 (Brace Expansion)?

The time complexity for LeetCode 1087 (Brace Expansion) varies by solution. Check the detailed explanation section for specific complexities in Java, C++, and Python implementations.

Can I run code for LeetCode 1087 on DevExCode?

Yes, DevExCode provides an interactive code editor where you can write, test, and run your code for LeetCode 1087 in Java, C++, or Python.

Back to LeetCode Solutions