BackmediumStack Atlassian

Generate Parenthesis Combinations Solution

Problem Statement

Given a string containing only '(', ')', and '*', find all unique valid combinations of parentheses where every open parenthesis can be matched with a corresponding close parenthesis or a '*'. The result should be sorted in lexicographic order.

Example 1
Input
1
Output
["()"]

Explanation: For a single pair of parentheses, there is only one valid combination.

Example 2
Input
2
Output
["(())", "()()"]

Explanation: For two pairs of parentheses, there are two valid combinations with 1 pair open and the other two closed.

Constraints

  • 1 <= n <= 8
  • The input is an integer representing the number of pairs of parentheses.
  • The output should be a list of strings, each representing a valid combination of parentheses.
  • The output list should be sorted in lexicographic order.
  • The input string may contain '*' which can be considered as either '(' or ')'
Live Compiler
Loading...
Test Cases & Output
🔒 Sign up to run your code

🚀 Practice this problem

Run code, get AI hints & track streak

Sign Up Free