Skip to content

Instantly share code, notes, and snippets.

@shawndumas
Last active October 7, 2015 16:19
Show Gist options
  • Save shawndumas/8867467 to your computer and use it in GitHub Desktop.
Save shawndumas/8867467 to your computer and use it in GitHub Desktop.
IsBalanced
const isBalanced = (s, open, close) => {
return !!(
s.split('').reduce((p, c, i) => {
if (c === close && i === 0 || p === false) { p = false; }
else if (c === open ) { p += -1; }
else if (c === close) { p += 1; }
return p;
}, 0)
);
};
// isBalanced(')(', '(', ')'); => false
// isBalanced('(()())', '(', ')'); => true
// isBalanced('(()(())', '(', ')'); => false
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment