This practice workspace is built for desktop. Open it on a larger screen to start solving.
Generate Parentheses
Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses.
A string is well-formed iff at every prefix the number of '(' is at least the number of ')', and the totals match overall. The classic backtracking formulation tracks open and close counts and prunes whenever open > n or close > open.
Return the strings sorted in ascending lexicographic order. Note that under standard ASCII ordering '(' < ')', so "(())" sorts before "()()".
By convention, n = 0 returns the list containing the empty string: [""].
Examples
Constraints
- 0 <= n <= 8
Preview Mode
This is a read-only preview of DSAMind's AI coaching. Sign up to get custom feedback on your own solution.
Your approach
- Pattern:Backtracking
- Time:O(N)
- Space:O(1)
Complete approach — pattern and complexity both named.
Coach
What you got right
You correctly matched the sorted input requirement and implemented a linear-time scan using two pointers.
Where it diverged
No divergence detected. The code correctly aligns with the stated approach.
Next attempt: focus on
Try solving related sliding window problems to build familiarity with two-pointer variants.
Signals
No signals fired — clean run, you stayed in flow.