Skip to content

Instantly share code, notes, and snippets.

@crouch74
Created August 9, 2017 13:25
Show Gist options
  • Save crouch74/96f8baef8b0053881f217ead83d27597 to your computer and use it in GitHub Desktop.
Save crouch74/96f8baef8b0053881f217ead83d27597 to your computer and use it in GitHub Desktop.
balanced parentheses js solution
function isValid(s) {
var stack = [];
for (var i = 0; i < s.length; i++) {
var c = s[i];
if (c == '(' || c== '[' || c == '{') {
stack.push(c);
continue;
}
if (c == ')' || c== ']' || c == '}') {
var l = stack.pop();
if (
(c == ']' && l == '[') ||
(c == '}' && l == '{') ||
(c == ')' && l == '(')
) {
continue;
}
return false;
}
}
return stack.length == 0
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment