Brackets   A
last analyzed

Complexity

Total Complexity 12

Size/Duplication

Total Lines 32
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Importance

Changes 0
Metric Value
wmc 12
c 0
b 0
f 0
lcom 0
cbo 0
dl 0
loc 32
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
C solution() 0 29 12
1
<?php
2
3
namespace Lesson05;
4
5
class Brackets
6
{
7
    public function solution($S)
8
    {
9
        $length = strlen($S);
10
        $map = [')' => '(', '}' => '{', ']' => '['];
11
        if ($length) {
12
            $parentheses = [];
13
            for ($i = 0; $i < $length; $i++) {
14
                $c = $S[$i];
15
                if ($c == '(' || $c == '{' || $c == '[') {
16
                    array_push($parentheses, $c);
17
                } elseif ($c == ')' || $c == '}' || $c == ']') {
18
                    if (empty($parentheses)) {
19
                        return 0;
20
                    }
21
                    $v = array_pop($parentheses);
22
                    if ($map[$c] != $v) {
23
                        return 0;
24
                    }
25
                }
26
            }
27
            if (empty($parentheses)) {
28
                return 1;
29
            } else {
30
                return 0;
31
            }
32
        }
33
34
        return 1;
35
    }
36
}
37