Skip to content

Instantly share code, notes, and snippets.

@c-mac
Created June 24, 2014 17:19
Show Gist options
  • Save c-mac/543f756bfdcc67e34477 to your computer and use it in GitHub Desktop.
Save c-mac/543f756bfdcc67e34477 to your computer and use it in GitHub Desktop.
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;
}
@bcjordan
Copy link

bcjordan commented Dec 3, 2014

How about the input [?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment