@@ -0,0 +1,1328 @@
+/*%0A Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses.%0A%0AFor example, given n = 3, a solution set is:%0A%0A%22((()))%22, %22(()())%22, %22(())()%22, %22()(())%22, %22()()()%22 %0A*/%0A%0Aimport java.io.*;%0Aimport java.util.*;%0A%0Apublic class GenerateParentheses %7B%0A    public static List%3CString%3E res = new ArrayList%3CString%3E();%0A%09public static List%3CString%3E generateParenthesis(int n) %7B%0A        if(n %3C= 0) return res;%0A        helper(1, 0, 1, %22(%22, n);%0A        return res;%0A    %7D%0A    %0A    public static void helper(int left, int right, int L, String s, int n)%7B%0A        if(right %3E left) return;%0A        if(left == n && right == n)%7B%0A            res.add(s);%0A            return;%0A        %7D%0A        %0A        for(int i = L+1; i %3C= 2*n; i++)%7B%0A            if(left %3C n) helper(left+1, right, i, s+%22(%22, n);%0A            if(right %3C left)%7B%0A               helper(left, right+1, i, s+%22)%22, n); %0A            %7D%0A        %7D%0A        return;%0A    %7D%09%0A%09%0A%09public static void main(String%5B%5D args) %7B%0A%09%09System.out.println(%22Generate pairs of parenthesis%22);%0A%09%09Scanner reader = new Scanner(System.in); %0A%09%09System.out.println(%22Enter a number: %22);%0A%09%09int n = reader.nextInt();%0A%09%09List%3CString%3E res = generateParenthesis(n);%0A%09%09System.out.println(%22Results: %22);%0A%09%09for(int i = 0; i %3C res.size(); i++)%7B%0A%09%09%09System.out.print(%22 %22 + res.get(i));%0A%09%09%7D%0A%09%09return;%0A%09%7D%09%0A%7D%0A

generate parentheses