Package Level3; import java. util. arrayList;/*** Generate Parentheses *** 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 S22 {public static void main (String [] args) {System. out. println (generateParenthesis (3);} public static ArrayList <String> generateParenthesis (int n) {ArrayList <String> list = new ArrayList <String> (); rec (n, 0, 0, "", list); return list;} public static void rec (int n, int left, int right, String s, ArrayList <String> list) {// The number of invariant must be greater than or equal to the number of right brackets. if (left <right) {return ;} // If the left and right parentheses are equal, add them to listif (left = n & right = n) {list. add (s); return;} // The left parenthesis is full. You can only add the right brace if (left = n) {rec (n, left, right + 1, s + ") ", list); return;} rec (n, left + 1, right, s +" (", list); // Add the left brace rec (n, left, right + 1, s + ")", list); // Add another right bracket }}