An n parentheses sequence consists of n
(s and n
A valid parentheses sequence is defined as the following:
You can find a way to repeat erasing adjacent pair of parentheses "()" until it becomes empty.
(())is a valid parentheses, you can erase the pair on the 2nd and 3rd position and it becomes
(), then you can make it empty.
)()(is not a valid parentheses, after you erase the pair on the 2nd and 3rd position, it becomes
)(and you cannot erase any more
Given a number n you need to generate all correct parenthesis sequence in lexicographical order
Output can be an array, list or string (in this case a sequence per line)
You can use a different pair of parenthesis such as
() or any open-close sign
n = 3
((())) (()()) (())() ()(()) ()()()
n = 2