Created
April 6, 2016 17:54
-
-
Save evaldosantos/b68a86fab76cf6b0041553200914e63c to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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