Explanation
Generate Parentheses is a Medium interview problem tagged [String, Backtracking]. The winning approach is typically **Backtracking**.
#22
Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses.
Example 1
Input: n = 3
Output: ["((()))","(()())","(())()","()(())","()()()"]
All valid sequences with 3 pairs.
Example 2
Input: n = 1
Output: ["()"]
Only one valid string.
Generate Parentheses is a Medium interview problem tagged [String, Backtracking]. The winning approach is typically **Backtracking**.
Systematically covers the decision tree with pruning.
Build a partial solution; recurse; undo (pop) after exploring.
Pattern-level skeleton only — not this problem’s exact answer. Adapt it to the statement.
Time: O(2^n) – O(n!)
Space: O(n)
Before coding, restate the problem, name the pattern (Backtracking), and write the invariant or DP state in one sentence.
Got it?
Mark #22 Generate Parentheses as understood when the pattern clicks.