Challenge
Write a function areCurlyBracesMatched that takes in a string s containing only { and/or } and returns true if s is properly matched, and...
For further actions, you may consider blocking this person and/or reporting abuse
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
Python solution
OCaml solution:
Some outputs:
This solution skips the characters different from braces:
Rust:
Python
Functional JS with Tail Call Optimization (TCO)
Ahh, this old trick. I've done it for at least two interviewers this summer. Terrific way for them to determine how good I am at building Rails applications, but, I digress.
My solution in js
Simple JS solution
Iterate through the string and count up when you encounter a "{" and down when not.
Check after every iteration, if the last character was a closing brace too much and return false if so.
Finally, return true when the amount of opening braces matches the amount of closing braces (i == 0).
My take at the challenge written in Haskell.
Try it online.
I use a flag which is increased if it was '{' and decrease if it was '}'
Did you make sure that sequences like "}{" are correctly identified as invalid?
My mistake for this case. Many thanks :D
Elegant JS solution
I love this one.
Some F#
Damn, there is never any PHP solutions, I've to get to it !