title: Algorithm: optimizing 'balancing brackets'
body: <p><strong><em>I have been posed the following question...</em></strong></p>
  
  <p><em>Given a N different open and close braces in a string "( { [ } ] )", check whether the string has matching braces. Return true if the braces match, false otherwise.</em></p>
  
  <p><strong><em>Here is the answer I came up with...</em></strong></p>
  
  <pre><code>function braceeql(braces){
    var leftpar = 0; 
    var rightpar = 0; 
    var leftbrace = 0;
    var rightbrace = 0;
    var leftcurl = 0;
    var rightcurl = 0;
  
    for(var index = 0; index &lt; braces.length; index++){
      if(braces[index] == ')'){
        leftpar += 1;
      }else if(braces[index] == '('){
        rightpar += 1;
      }else if(braces[index] == '['){
        leftbrace += 1;
      }else if(braces[index] == ']'){
        rightbrace += 1;
      }else if(braces[index] == '{'){
        leftcurl += 1;
      }else if(braces[index] == '}'){
        rightcurl += 1;
      }
    }
    if(leftcurl == rightcurl &amp;&amp; leftbrace == rightbrace &amp;&amp; leftpar == rightpar){
      console.log(true)
    }else{
      console.log(false)
    }
  }
  </code></pre>
  
  <p>This is really meaty piece of code, but it sure as heck works. I am seeing  differing opinions about how others attacked this problem, but am left wondering <strong><em>is there a better/cleaner way of solving this algorithm without compromising the big O?</em></strong></p>
  
  <p>I am very open to suggestions and other ways of looking at this problem. </p>