Ctrl K

Expanding Curly Braces

Hard
A template describes a set of words made of lowercase English letters, following these rules: - a single letter describes a one-word set: "q" → {q}; - curly braces holding one or more comma-separated templates describe the union of their sets: "{a,bc,a}" → {a, bc}; - templates written one after another describe every concatenation where the first piece comes from the first template's set and the second from the second's. For example, "{a,b}{c,d}" → {ac, ad, bc, bd}, and "m{e,a}t" → {met, mat}. The rules can be nested to any depth. Return every distinct word described by the template, in lexicographic order.
Example 1
template="{x,yz}{a,b}" → ['xa', 'xb', 'yza', 'yzb']

Each word of the first group is joined with each word of the second: xa, xb, yza, yzb.

Example 2
template="k{o,{o,i}t}" → ['kit', 'ko', 'kot']

The inner part gives "ot" and "it", together with "o" that makes ko, kot, kit.

Example 3
template="{a,b,a}" → ['a', 'b']

Duplicates appear in the answer only once.

Constraints
  • 1 ≤ len(template) ≤ 60
  • template consists of {, }, , and lowercase English letters
  • template is well formed under the rules above
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsClear the Marble Row HardEvery Spelling of a Pattern MediumUp-Down Digit Code MediumLongest Balanced Bracket Stretch HardOne Letter at a Time Hard
loading Python…
Input
template="{x,yz}{a,b}"
Expected
['xa', 'xb', 'yza', 'yzb']

Run checks the first 3 tests in your browser. Submit checks all 92 tests on the server, hidden ones included.