Categories
interview

Generate Paranthesis – Java

class Solution {
    List<String> list;
    public List<String> generateParenthesis(int n) {
        list = new ArrayList<String>();
        genP("", n, n);
        return list;
    }
    
    public void genP(String str, int open, int closed) {
        if (closed == 0)
        {    list.add(str);
              return;
        }  else {
              if (closed > open) {
                genP(str + ')', open, closed - 1);
               }
        if(open >= 1)
            genP(str + '(', open - 1, closed);
        }      
    }
}