-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path856.php
36 lines (34 loc) · 784 Bytes
/
856.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
<?php
class Solution
{
function scoreOfParentheses($s)
{
$len = strlen($s);
// return $s;
//(11)(((1)))1
$ans=[];
$cnt=0;
$w=0;
for ($i=0; $i < $len; $i++) {
$cnt=0;
if($s[$i]=='('){
array_push($ans, 0);
}
if($s[$i]==')')
{
while($ans[array_key_last($ans)]!=0){
$cnt+=array_pop($ans);
}
array_pop($ans);
$temp=max(2*$cnt,1);
array_push($ans,$temp);
}
}
return array_sum($ans) ;
}
}
$obj = new Solution();
$s = "(()())((()))()"; // "a(b(c)d)" ((())
echo "<pre>";
print_r($obj->scoreOfParentheses($s));
echo "</pre>";