Skip to content

Instantly share code, notes, and snippets.

@evaldosantos
Created April 6, 2016 17:54
Show Gist options
  • Save evaldosantos/b68a86fab76cf6b0041553200914e63c to your computer and use it in GitHub Desktop.
Save evaldosantos/b68a86fab76cf6b0041553200914e63c to your computer and use it in GitHub Desktop.
function Stack(tArr) {
var arr = tArr || [];
this.push = function(n) {
arr.push(n);
return this;
};
this.pop = function() {
arr.pop();
return this;
};
this.empty = function() {
return arr.length==0;
}
this.top = function() {
return arr[arr.length-1];
}
}
function validParentheses(parens){
var stack = new Stack();
parens.split('').forEach(function(el) {
if(el==')' && stack.top() != '(') {
stack.push(el);
} else if(el=='(') {
stack.push(el);
} else if(el==')') {
stack.pop();
}
})
return stack.empty();
}
Test.assertEquals(validParentheses( "()" ), true);
Test.assertEquals(validParentheses( "())" ), false);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment