Leetcode Problem 1096. Brace Expansion II

1096. Brace Expansion II

Leetcode Solutions

Iterative Stack-based Solution

  1. Initialize an empty stack, an empty result list, and an empty current list.
  2. Iterate through each character in the expression.
    • If the character is a letter, append it to each string in the current list or start a new list if the current list is empty.
    • If the character is '{', push the result and current lists onto the stack and reset them.
    • If the character is '}', pop the previous lists from the stack, perform a cartesian product with the current list, and update the result list.
    • If the character is ',', add the current list to the result list and reset the current list.
  3. After the iteration, add the remaining current list to the result list.
  4. Convert the result list to a set to remove duplicates, sort it, and return the sorted list.
UML Thumbnail

Recursive Descent Parsing Solution

Ask Question

Programming Language
image/screenshot of info(optional)
Full Screen
Loading...

Suggested Answer

Answer
Full Screen
Copy Answer Code
Loading...