Skip to main content
PreparationZone Logo
PreparationZone
Back to Coding

Coding · Medium · Pro

Generate all combinations of well-formed parentheses for n pairs

CodingRecursionBacktrackingSenior
Model answerPreview

Approach

  • Track open and close counts; add '(' if open < n.
  • Add ')' only if close < open.

Java Solution

Java
void gen(int n, int open, int close, StringBuilder sb, List<String>…