Lesson 5 – Stacks and Queues – Brackets demo task solution code written in php. Gives 100/100 score at the time of publishing. The description of the problem is copyrighted, so please see the following link for it: //codility.com/demo/take-sample-test/brackets
Solution in php:
function solution($S) { if(empty($S)){ return 1; } $S = str_split($S); $matched = array("]" => "[", "}" => "{", ")" => "("); $to_push = array("[", "{", "("); $stack = array(); foreach($S as $v){ if(in_array($v, $to_push)){ $stack[] = $v; } else{ if(!count($stack)){ return 0; } elseif($matched[$v] != array_pop($stack)){ return 0; } } } return !count($stack) ? 1 : 0; }
Given “AS IS”, can be ported from other languages from solutions found on the internet, please use with care.
Please note: we think that codility.com does not give a correct assessment of your real-world programming skills. For instance, reading the below would bring more understanding why: //codility-test-questions.blogspot.com/2013/01/my-experience-with-codility-test.html