Skip to content

Practice Brace Expansion II

Read the task, work through the examples, and check your approach against the constraints. Use the original LeetCode task for coding and submissions.

Hard#1096
LeetCode Problem

Brace Expansion II

Problem Statement

Interpret expression as a set of lowercase words. A single letter represents the set containing that letter. Braces around a comma-separated list represent the union of the enclosed expressions, removing duplicates. Concatenated expressions represent every concatenation of a word from the first set with a word from the second set. These rules apply recursively to nested expressions. Return the distinct resulting words in sorted order.

Source: Brace Expansion II on LeetCode. Use the original task for its coding environment and submissions.

Examples

# Example 1

Input
expression = "{a,b}{c,{d,e}}"
Output
["ac","ad","ae","bc","bd","be"]

# Example 2

Input
expression = "{{a,z},a{b,c},{ab,z}}"
Output
["a","ab","ac","z"]

Constraints

1 <= expression.length <= 60
expression[i] consists of '{', '}', ','or lowercase English letters.
The given expression represents a set of words based on the grammar given in the description.

Related LeetCode Problems

Practice more problems similar to Brace Expansion II.

Review your practice with PhantomCodeAI

After trying the problem yourself, use the app to discuss your approach, complexity, and edge cases.