Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses.
For example, given n = 3, a solution set is:
[ "((()))", "(()())", "(())()", "()(())", "()()()"]
public class Solution {
List<String> res = new ArrayList<String>();
public List<String> generateParenthesis(int n) {
helper(n,n,"");
return res;
}
public void helper(int left,int right,String tmp)
{
if(right<left)
return;
if(left==0&&right==0)
{
res.add(tmp);
return;
}
if(left>0)
helper(left-1,right,tmp+"(");
if(right>left)
helper(left,right-1,tmp+")");
}
}