Solution to LeetCode Generate Parenthesis question.
There are two solutions I can think of: one based on the BNF representation, and one based on counting how many parenthesis we have left.
The detailed explanation can be found here and here
The following Java code uses the BNF approach: