Skip to content

Instantly share code, notes, and snippets.

@spirinvladimir
Created April 17, 2026 06:36
Show Gist options
  • Select an option

  • Save spirinvladimir/0ba9819a9114d35d507ce347427421f0 to your computer and use it in GitHub Desktop.

Select an option

Save spirinvladimir/0ba9819a9114d35d507ce347427421f0 to your computer and use it in GitHub Desktop.
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
}
@spirinvladimir
Copy link
Copy Markdown
Author

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: ()[]{}.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment