Balance String - Node.js String

Node.js examples for String:Algorithm

Description

Balance String

Demo Code

String.prototype.balance = function(open, close) {
  var i = 0;/*w w  w.j  av  a  2  s  .c  o m*/
  while (this.charAt(i) != open) {
    if (i == this.length) return [-1, -1];
    i++;
  }
  
  var j = i+1;
  var balance = 1;
  while (j < this.length) {
    if (this.charAt(j) == open) balance++;
    if (this.charAt(j) == close) balance--;
    if (balance == 0) break;
    j++;
    if (j == this.length) return [-1, -1];
  }
  
  return [i, j];
}
/*t:
  plan(16, "Testing String.prototype.balance.");
  
  var s = "{abc}".balance("{","}");
  is(s[0], 0, "opener in first is found.");
  is(s[1], 4, "closer in last is found.");
  
  s = "ab{c}de".balance("{","}");
  is(s[0], 2, "opener in middle is found.");
  is(s[1], 4, "closer in middle is found.");
  
  s = "a{b{c}de}f".balance("{","}");
  is(s[0], 1, "nested opener is found.");
  is(s[1], 8, "nested closer is found.");
  
  s = "{}".balance("{","}");
  is(s[0], 0, "opener with no content is found.");
  is(s[1], 1, "closer with no content is found.");
  
  s = "".balance("{","}");
  is(s[0], -1, "empty string opener is -1.");
  is(s[1], -1, "empty string closer is -1.");
  
  s = "{abc".balance("{","}");
  is(s[0], -1, "opener with no closer returns -1.");
  is(s[1], -1, "no closer returns -1.");
  
  s = "abc".balance("{","}");
  is(s[0], -1, "no opener or closer returns -1 for opener.");
  is(s[1], -1, "no opener or closer returns -1 for closer.");
  
  s = "a<bc}de".balance("<","}");
  is(s[0], 1, "unmatching opener is found.");
  is(s[1], 4, "unmatching closer is found.");
*/

Related Tutorials