Given an integer n
, generate all strings with n
matching parentheses. “matching” parentheses mean
- There is equal number of opening and closing parentheses.
- Each opening parenthesis has matching closing parentheses.
For example, ()
is a valid string but )(
is not a valid string because )
has no matching parenthesis before it and (
has no matching parenthesis after it.
Solution
This is a Backtracking problem (keywords: “generate all”). Starting with an empty string, we can add (
or )
and continue to do so until the length reaches 2 * n
.
Generated strings are invalid when:
- There are more than
n
occurrences(
- We want to add a
)
but there is no matching(
We can prune branches that don’t lead to a valid leaf node.