Created
April 17, 2026 06:36
-
-
Save spirinvladimir/0ba9819a9114d35d507ce347427421f0 to your computer and use it in GitHub Desktop.
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
| function validBraces(braces){ | |
| if (braces.length < 2) return false | |
| var open = new Set(["{", "(", "["]) | |
| var match = (a, b) => { | |
| if (a == '{' && b == '}') return true | |
| if (a == '[' && b == ']') return true | |
| if (a == '(' && b == ')') return true | |
| return false | |
| } | |
| var stack_open = [] | |
| var i = 0 | |
| while (i < braces.length) { | |
| var b = braces[i] | |
| if (open.has(b)) { | |
| stack_open.push(b) | |
| } else { | |
| if (!match(stack_open.pop(), b)) return false | |
| } | |
| i++ | |
| } | |
| return stack_open.length == 0 | |
| } |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Write a function that takes a string of braces, and determines if the order of the braces is valid. It should return true if the string is valid, and false if it's invalid.
This Kata is similar to the Valid Parentheses Kata, but introduces new characters: brackets [], and curly braces {}. Thanks to @arnedag for the idea!
All input strings will be nonempty, and will only consist of parentheses, brackets and curly braces: ()[]{}.