Given an integer n, return every well-formed parentheses string that uses exactly n pairs of parentheses.
Return the answers in the deterministic order produced by backtracking that always tries appending ( before ) whenever both choices are allowed.
Input / output
n: intstring[] containing every valid combination in canonical backtracking orderExamples
n = 3 returns ["((()))","(()())","(())()","()(())","()()()"].n = 1 returns ["()"].n = 0 returns [""] because there is exactly one valid sequence of zero pairs: the empty string.Constraints
0 <= n <= 7Target complexity
2^(2n) raw strings.Follow-up How would you count the number of valid combinations without generating them all, or generate them iteratively instead of recursively?