Brace Expansion II
A pattern describes a set of strings. A letter is a literal. A brace group {e1,e2,...} is the union of its comma-separated sub-expressions, and writing expressions next to each other is a product (every string from the first followed by every string from the second). Groups can nest to any depth.
Return all distinct strings the pattern produces, sorted.
"{a,b}{c,{d,e}}" -> ["ac", "ad", "ae", "bc", "bd", "be"]
"{{a,z},a{b,c},{ab,z}}" -> ["a", "ab", "ac", "z"]
"abc" -> ["abc"]Hints
Hint 1
Write the grammar as a comment first: expr := term (',' term)* is a union, term := factor+ is a product, factor := letter | '{' expr '}'.
Hint 2
Each rule becomes one small function returning a set of strings, sharing one position pointer into the pattern.
Hint 3
Sets give deduplication for free; sort once at the end.