Created
June 24, 2014 17:19
-
-
Save c-mac/543f756bfdcc67e34477 to your computer and use it in GitHub Desktop.
Problem can also be found here: https://www.hackerrank.com/contests/programming-interview-questions/challenges/balanced-delimiters
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 isValid(s) { | |
var stack = []; | |
var rightChars = [')', '}', ']']; | |
for (var i = 0; i < s.length; i++) { | |
var curr = s[i]; | |
if (stack.length > 0 && isInArray(curr, rightChars)) { | |
var top = stack.pop(); | |
if (curr === ')' && top !== '(') return false; | |
if (curr === ']' && top !== '[') return false; | |
if (curr === '}' && top !== '{') return false; | |
} | |
else { | |
if (curr !== ' ') stack.push(curr); | |
} | |
} | |
return true; | |
} | |
function isInArray(value, A) { | |
return A.indexOf(value) > -1; | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
How about the input
[
?