Created
July 29, 2016 07:42
-
-
Save LiuuY/162a478ef6463a8eb4af327ec15382ab to your computer and use it in GitHub Desktop.
Valid Braces
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 validBraces(braces){ | |
var matches = { '(':')', '{':'}', '[':']' }; | |
var stack = []; | |
var currentChar; | |
for (var i = 0; i < braces.length; i++) { | |
currentChar = braces[i]; | |
if (matches[currentChar]) { // opening braces | |
stack.push(currentChar); | |
} else { // closing braces | |
if (currentChar !== matches[stack.pop()]) { | |
return false; | |
} | |
} | |
} | |
return stack.length === 0; // any unclosed braces left? | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment