title: print all validate parentheses, how does the recursive work here?
body: <p>Implement an algorithm to print all valid (e.g., properly opened and closed) combinations of n-pairs of parentheses.
  EXAMPLE:
  input: 3 (e.g., 3 pairs of parentheses)
  output: ()()(), ()(()), (())(), ((()))</p>
  
  <p>the answer is :</p>
  
  <pre><code>private static void printPar(int count)
  {
      char[] str = new char[count*2];
      printPar(count,count,str, 0);
  }
  
  private static void printPar(int l, int r, char[] str, int count)
  {
      if(l &lt; 0 || r &lt; l)
          return;
  
      if (l ==0 &amp;&amp; r == 0)
      {
          System.out.println(str);
      }
      else
      {
          if (l &gt; 0 )
          {
              str[count] = '(';
              printPar(l-1, r, str, count + 1);
          }
  
          if (r &gt; 0)
          {
              str[count] = ')';
              printPar(l, r-1, str, count + 1);
          }
      }
  }
  </code></pre>
  
  <p>But i dont quite fully understand the solution although someone claims the explanation is straightforward enough. (this code works fine)</p>
  
  <p>In my opinion, this code works as when there is more left parentheses, then add the left parentheses. so, only condition of （（（））） coz the condition 
      if (l > 0 )
  appear before r > 0 , so, it should always handle all the left ones first.</p>
  
  <p>But how this code handle this situation "()(())"? i debug this code, and find out that after it prints out "((()))". it went to the situation of l =1, r =3, and str="((()))" and count = 2. which doesnt make sense to me.</p>
  
  <p>Also, if someone can explain what is the time/space complexity, that would be much helpful for me.</p>
  
  <p>Thanks in advance.</p>