Question: http://codility.com/demo/take-sample-test/brackets
Question Name: Brackets
Classic application of stacks.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | def solution(S): if len(S) % 2 == 1: return 0 matched = {"]":"[", "}":"{", ")": "("} to_push = ["[", "{", "("] stack = [] for element in S: if element in to_push: stack.append(element) else: if len(stack) == 0: return 0 elif matched[element] != stack.pop(): return 0 if len(stack) == 0: return 1 else: return 0 |
UPDATE (2016-07-19): Much appreciate Jeffrey’s comment and suggestion:
I’m surprised so few people didn’t do a simple modulo at the start to check whether there is an even number of chars. In production, you wouldn’t want this algorithm to run against a big string and only terminate at the end when one closing token is missing. You’d want it to terminate as soon as it’s possible
Ruby Version
oops, how do I embed code in a comment?
Thanks for your sharing! Please place the code inside a <pre> </pre> block.
C# Solution.
Thanks for sharing!
I don’t know if you’ve tested this in a IDE, but this is the correct C# solution 100%:
Thanks for sharing! I tested my code in both local IDE and Codility.com.
Above code had compilation errors even algorithm is correct but does not compile correctly. I will suggest the code below:
See the results for this over the codility:
https://codility.com/demo/results/demo4KD3BK-X89/
Thank you!
Interestingly, in C#, if you use generic
to avoid all “.ToString()”s same code will fail on codility.com by performance – shows only 40%. Changing it back to
gives 100%.
I was talking about Kaiser’s solution. Apparently “<pre>” tag somehow formatted my Dictionary<string,string> VS 2013 won’t recognize Dictionary as a Generic Collection
I do not know VS very much. But probably you cannot directly copy some code in the comment section, because they escape some HTML Character Entities like < and >.
it’s because you have to add :
using System.Collection.Generic.
Another working solution in csharp :
https://codility.com/demo/results/demoDHU8ZZ-DBB/
Thanks for your explanation!
This works same as a stack and it is a bit shorter (still O(n)):
Good idea. Actually you are doing a stack by your self.
For the other readers with other programming languages, please notice that the top pointer might be negative value. For example, the test input might be “)))”.
Very nice solution!
Here’s a Java 100/100 Solution:
https://codility.com/demo/results/demo97TPVG-CPP/
A C# solution:
I’m surprised so few people didn’t do a simple modulo at the start to check whether there is an even number of chars. In production, you wouldn’t want this algorithm to run against a big string and only terminate at the end when one closing token is missing. You’d want it to terminate as soon as it’s possible
Much appreciate your comment and suggestion!
Py:
Swift solution 100%
Completely uneccessary solution using numbers. Python solution – O(N).
Thanks for this blog post! I was banging my head against the wall thinking this is a simple / short solution, where you just build a stack and scan each item popped for validity.
Your solution is way more elegant than mine but I set out not to use anything as complex as a dictionary.