JavaScript Recursive Solution


  • 0
    I
    function generateParenthesis(n) {
      var result = [];
      
      function addPar(str, n, m) {
        if(n === 0 && m === 0) {
          result.push(str);
          return;
        }
    
        if(m > 0) {
          addPar(str + ")", n, m-1);
        }
    
        if(n > 0) {
          addPar(str + "(", n-1, m+1);
        }
      }
      
      addPar('', n, 0);
      return result;
    }
    

Log in to reply
 

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