Challenge Taken from here and also here
An n parentheses sequence consists of n (
s and n )
s.
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.
For example,
(())
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
Task
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
Example
n = 3
((())) (()()) (())() ()(()) ()()()
n = 2
(()) ()()
1
s and-1
s)? \$\endgroup\$