Challenge
Write a function areCurlyBracesMatched
that takes in a string s
containing only {
and/or }
and returns true
if s
is properly matched, and false
otherwise.
A string s
is considered properly matched if the string is empty or if every open brace has a corresponding close brace.
Examples
{{}}
is properly matched.
{{{
is not properly matched.
{}{}{}
is properly matched.
areCurlyBracesMatched("{{{}{}}}"), true;
areCurlyBracesMatched("{{"), false;
areCurlyBracesMatched("{}}"), false;
areCurlyBracesMatched(""), true;
Happy coding!
This challenge comes from theonlydaryl on CodeWars. Thank you to CodeWars, who has licensed redistribution of this challenge under the 2-Clause BSD License!
Want to propose a challenge idea for a future post? Email yo+challenge@dev.to with your suggestions!
Top comments (35)
Here we go!
Read character by character, add 1 to a counter if the character is an opening bracket, subtract 1 if it is a closing one. If the counter EVER gets negative (hence, if we get in an "open close close" situation) of if it is positive on the last iteration (hence, if we get in an "open open close" situation), we consider the chain as invalid.
Why you post the uglified code?
I just try to find a really short working solution, hence the uglified code! That's why I try to explain it as much as I can after that
I think that these kind of challenges prior more the algorithm part than the gulfing part. Since ES6, JavaScript has became a good language for gulfing but sometimes self explanatory code is better than gulfed one.
Its a matter of taste I guess.
Exactly! As a matter of fact, I prefer Alfredo Salzillo's solution as it is a clear and pretty oneliner. On these challenges I try to come up with solutions different than a straight-forward approach (and tend to golf it too), which I couldn't manage to do on this one. That explains the ugly part of the solution I provided, which is kinda disappointing!
One line JS
I like this type of challenges that encourage the use of queues or/and stacks concepts. Anyway, this is my solution that suggests an early exist as soon as a brace does not match.
Hey! I didn't know we could embed repl here. Looking dope!
Yup, liquid tags are fun 🦄
Here we Go!... I swear I'm funny sometimes
curly.go
curly_test.go
Another one line JS solution
You made a typo:
Math.flor
should beMath.floor
. Otherwise good take at using a regular expression. Unfortunately, it does not work for cases likeAs the curly braces are separated by other characters.
For this challenge the string can only contain '{' or '}', so your example is not a valid input.
It's obvious that it can't be resolved with a regexp for the general case.
Smart! I like it!
Let's see what you guys think...
C++
With the examples given, this challenge is identical to Challenge 29: Xs and Os, because counting the braces suffice to get the answer.
If you add
areCurlyBracesMatched("}{"), false;
, though, it’s different.I think it’s a pity that the examples for a lot of these challenges are missing some important cases that would help when the explanation is not completely clear.
Perl solution.
Just remove all the
{}
s until there's nothing to remove. If you got an empty string, the input was valid.ruby <3