SHORT SOLUTION BACKTRACK JAVA 10 lines


  • 0
    T
    public class Solution {
    List<String> result=new ArrayList<String>();
    public void assGen(String s, int left, int right){
        if(left==0&&right==0)result.add(s);
        else if(left==right)assGen(s.concat("("),left-1,right);
        else {
            if(left>0)assGen(s.concat("("),left-1,right);
            assGen(s.concat(")"),left,right-1);
        }
    }
    public List<String> generateParenthesis(int n) {
        assGen("",n,n);
        return result;
    }
    

    }


Log in to reply
 

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