Last active
August 29, 2015 14:23
-
-
Save SeeThruHead/ce0eee15f716d58d73b1 to your computer and use it in GitHub Desktop.
Balanced Parens
This file contains 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
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