Created
January 10, 2017 18:03
-
-
Save josenaves/d9f82387d5ecc07d0bb739a5cf60b9b4 to your computer and use it in GitHub Desktop.
Balanced parenthesis problem
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
function balancedParentesis(string) { | |
return !string.split("").reduce(function(previous, char) { | |
if (previous < 0) return previous; | |
if (char === "(") return previous + 1; | |
if (char === ")") return previous - 1; | |
return previous; | |
}, 0); | |
} | |
balancedParentesis("))(("); // False | |
balancedParentesis(")"); // False | |
balancedParentesis("()"); // True | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment