-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path20.php
More file actions
35 lines (33 loc) · 853 Bytes
/
20.php
File metadata and controls
35 lines (33 loc) · 853 Bytes
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
<?php
//kc
class Solution
{
/**
* @param String $s
* @return Boolean
*/
function isValid($s)
{
$len = strlen($s);
$ans=[];
for ($i = 0; $i < $len; $i++) {
if ($s[$i] == '('|| $s[$i] == '{'|| $s[$i] == '[') {
array_push($ans,$s[$i]);
} else if ($s[$i] == ')' && $ans[array_key_last($ans)]=='(') {
array_pop($ans);
}
else if ($s[$i] == '}'&& $ans[array_key_last($ans)] == '{') {
array_pop($ans);
}
else if ($s[$i] == ']'&& $ans[array_key_last($ans)] == '[') {
array_pop($ans);
}
else {
return false;
}
}
return count($ans)>0?false:true;
}
}
$obj=new Solution();
echo $obj->isValid("()[]");