My JavaScript Solution


  • 2
    I
    var generateParenthesis = function(n) {
    
    var leftArr = [], rightArr = [], count, result = [];
    
    for (count = 0; count < n; count++) {
        leftArr.push("(");
        rightArr.push(")");
    }
    
    function myFunction (left, right, arr) {
        
        //Boundary
        if (left <= 0) {
        
            if (right > 0) {
                var  i;
                for (i = 0; i < right; i++) {
                    arr += ")";
                }
            }
            result.push(arr);
            return;
        }
        
        myFunction (left - 1, right + 1, arr + "(");
        if (right > 0) {
            myFunction (left, right - 1, arr + ")");
        }
    }
    
    myFunction (n-1, 1, "(");
    return result;
    
    };

Log in to reply
 

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