Created
November 13, 2016 16:19
-
-
Save railsstudent/3da0e34efd1389c6ed17946dd1357159 to your computer and use it in GitHub Desktop.
Valid Braces kata published in codewars
This file contains hidden or 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
validBraces = (braces) -> | |
bracesMap = { | |
']': '[', | |
')': '(', | |
'}': '{' | |
} | |
stack = []; | |
for i in [0...braces.length] | |
if braces[i] in ['(', '{', '['] then stack.push braces[i] | |
if braces[i] in [')','}', ']'] | |
openBrace = bracesMap[braces[i]] | |
if stack[stack.length - 1] isnt openBrace then return false | |
else stack.pop() | |
return stack.length is 0 | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment