Code Self Study Forum

Puzzle: Valid Braces [algorithms]

Valid Braces” is a coding puzzle that people can be attempted in the following languages: javascript, coffeescript, haskell, csharp, python, java, ruby, php, typescript, elixir, cpp, dart, rust, swift, go, lua, and scala.

  • Difficulty: 6 kyu
  • Stars: 1510
  • Votes: 3498
  • Category: algorithms
  • Tags: Algorithms, Validation, Logic, Utilities
  • Source: codewars

Description

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

What is considered Valid?

A string of braces is considered valid if all braces are matched with the correct brace.

Examples


"(){}[]"   =>  True
"([{}])"   =>  True
"(}"       =>  False
"[(])"     =>  False
"[({})](]" =>  False

Solve It Here

Click the link below to solve it on Codewars:

Notes

This puzzle was posted by a Slackbot via a slash command. If you want to help work on the app, send a message to @Josh.

If you don’t want to see the coding puzzles when you visit the forum, you can go into your settings and mute the puzzles category.

1 Like

I did this one in Elixir. The #1 upvoted solution in Elixir was pretty interesting because of the way it used pattern matching.