Brace Expansion

MediumBacktrackingbacktracking

Solve this the way you would in the interview: out loud, on a timer, with an AI interviewer asking follow-ups while you code. You get a scored report at the end.

Practice this as a mock interviewFree to start, no account needed

Problem

You are given a pattern string s made of lowercase letters and brace groups. A brace group such as {a,b,c} lists alternative letters, and exactly one of them is chosen for that position. A letter outside braces is fixed. Groups are never nested. Return every word the pattern can produce, sorted in ascending lexicographic order. Every group has distinct options, so the result has no duplicates.

Examples

Input: s = "{a,b}c{d,e}f"
Output: ["acdf","acef","bcdf","bcef"]
The first group gives `a` or `b`, the second gives `d` or `e`; `c` and `f` are fixed.
Input: s = "abcd"
Output: ["abcd"]
There are no groups, so the only word is the pattern itself.

Constraints

  • 1 <= s.length <= 50
  • s contains lowercase letters, `{`, `}` and `,`
  • Every option inside a group is a single letter, and options in one group are distinct
  • Braces are balanced and never nested; every group has at least one option

What the interviewer grades

A correct solution is one of five scores. The report also grades problem understanding (did you clarify inputs and edge cases before coding), problem solving, communication while you work, code quality, and testing. See a sample report, or read up on the pattern in the lessons.

More backtracking problems