Nesting::solution()   C
last analyzed

Complexity

Conditions 7
Paths 10

Size

Total Lines 24
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 24
rs 6.7272
cc 7
eloc 16
nc 10
nop 1
1
<?php
2
3
namespace Lesson05;
4
5
class Nesting
6
{
7
    public function solution($S)
8
    {
9
        $length = strlen($S);
10
        if ($length) {
11
            $parentheses = [];
12
            for ($i = 0; $i < $length; $i++) {
13
                if ($S[$i] == '(') {
14
                    array_push($parentheses, '(');
15
                } elseif ($S[$i] == ')') {
16
                    if (empty($parentheses)) {
17
                        return 0;
18
                    }
19
                    array_pop($parentheses);
20
                }
21
            }
22
            if (empty($parentheses)) {
23
                return 1;
24
            } else {
25
                return 0;
26
            }
27
        }
28
29
        return 1;
30
    }
31
}
32