Generate Parentheses

Question (LC.22)

The input n is a integer signifies the pairs of parentheses. Write a function that returns a list of all possible valid formation of n pairs of parentheses.

Example

I: n = -1
O: []
I: n = 0
O: [""]
I: n = 1
O: ["()"]
I: n = 2
O: ["(())", "()()"]
I: n = 3
O: ["((()))", "(())()", "()(())", "(()())", "()()()"]

Analysis

all possible => exhaustive search

Code

Time & Space Complexity

Time O(2^n) Space O(2n)

Last updated