Expand a string with brace-delimited letter options into every word, in sorted order

Quick Overview

Expand a string in which curly braces list the letter choices for one position, and return every word it can form in sorted order. It tests string parsing, building a Cartesian product, and reasoning about lexicographic order.

Expand a string with brace-delimited letter options into every word, in sorted order

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Other

You are given a string `s` representing a list of words. Each letter in the word has one or more options. If there is one option, the letter is represented as is. If there is more than one option, curly braces delimit the options. For example, `"{a,b,c}"` represents the options `["a", "b", "c"]`. For example, if `s = "a{b,c}"`, the first character is always `'a'`, but the second character can be `'b'` or `'c'`, so `s` represents the list `["ab", "ac"]`. Return all words that can be formed this way, sorted in lexicographical order. ### Function Signature ```python def expand_braces(s: str) -> list[str]: ``` ### Rules - Outside braces, each letter is one fixed position in the word. A brace group `{...}` is also one position, and its comma-separated letters are the choices for that position. - The options inside a brace group may appear in any order in `s`. - Return the words sorted in ascending lexicographical order. Every word has the same length, and the letters inside a brace group are distinct, so the list has no duplicates. ### Constraints - `1 <= len(s) <= 50` - `s` consists of curly brackets `{}`, commas `,`, and lowercase English letters. - `s` is guaranteed to be a valid input. - There are no nested curly brackets. - All characters inside a pair of consecutive opening and closing curly brackets are different. ### Examples ```text Input: s = "{a,b}c{d,e}f" Output: ["acdf", "acef", "bcdf", "bcef"] ``` The word has four positions: a or b, then c, then d or e, then f. That gives four words, listed in sorted order. ```text Input: s = "abcd" Output: ["abcd"] ``` There are no braces, so the only word is "abcd". ```text Input: s = "{c,a}{b,a}" Output: ["aa", "ab", "ca", "cb"] ``` The first position is a or c and the second is a or b. The options are written out of order in `s`, and the output is still sorted.

Overview: Expand a string in which curly braces list the letter choices for one position, and return every word it can form in sorted order. It tests string parsing, building a Cartesian product, and reasoning about lexicographic order.

General logo
General
Nov 25, 2026
mediumSoftware EngineerOtherCoding & Algorithms
0
0

You are given a string s representing a list of words. Each letter in the word has one or more options.

If there is one option, the letter is represented as is. If there is more than one option, curly braces delimit the options. For example, "{a,b,c}" represents the options ["a", "b", "c"].

For example, if s = "a{b,c}", the first character is always 'a', but the second character can be 'b' or 'c', so s represents the list ["ab", "ac"].

Return all words that can be formed this way, sorted in lexicographical order.

Function Signature

def expand_braces(s: str) -> list[str]:

Rules

  • Outside braces, each letter is one fixed position in the word. A brace group {...} is also one position, and its comma-separated letters are the choices for that position.
  • The options inside a brace group may appear in any order in s .
  • Return the words sorted in ascending lexicographical order. Every word has the same length, and the letters inside a brace group are distinct, so the list has no duplicates.

Constraints

  • 1 <= len(s) <= 50
  • s consists of curly brackets {} , commas , , and lowercase English letters.
  • s is guaranteed to be a valid input.
  • There are no nested curly brackets.
  • All characters inside a pair of consecutive opening and closing curly brackets are different.

Examples

Input: s = "{a,b}c{d,e}f"
Output: ["acdf", "acef", "bcdf", "bcef"]

The word has four positions: a or b, then c, then d or e, then f. That gives four words, listed in sorted order.

Input: s = "abcd"
Output: ["abcd"]

There are no braces, so the only word is "abcd".

Input: s = "{c,a}{b,a}"
Output: ["aa", "ab", "ca", "cb"]

The first position is a or c and the second is a or b. The options are written out of order in s, and the output is still sorted.