Skip to content

Instantly share code, notes, and snippets.

@SeeThruHead
Last active August 29, 2015 14:23
Show Gist options
  • Save SeeThruHead/ce0eee15f716d58d73b1 to your computer and use it in GitHub Desktop.
Save SeeThruHead/ce0eee15f716d58d73b1 to your computer and use it in GitHub Desktop.
Balanced Parens
module.exports = function checkBal(input) {
function match(left, right) {
return left === '(' && right === ')';
}
function removeLast(ar) {
return ar.slice(0, ar.length - 1);
}
var result = input.split('').reduce(function(prev, curr, index, array) {
if (!prev.length) return [curr];
if (match(prev[prev.length -1], curr)) return removeLast(prev);
return prev.concat(curr);
}, []);
return result.length === 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment