Simple java solution


  • 0
    public class Solution {
        public List<String> generateParenthesis(int n) {
            int l=n,r=n;
            List<String> res=new ArrayList<>();
            String s="";
            solve(l,r,s,res);
            return res;
        }
        void solve(int l, int r,String s,List<String> res){
            if(l==0 && r==0){
                res.add(s);
            }
            if(l>0)
                solve(l-1,r,s+"(",res);
            if(r>l)
                solve(l,r-1,s+")",res);
        }
    }```

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.