$str = "([)]";
var_dump(isValid($str));
function isValid($s) {
$map = array(
"(" => ")",
"{" => "}",
"[" =>"]"
);
$len = strlen($s);
$stack = array();
for ($i = 0; $i < $len; $i ++) {
if (isset($map[$s[$i]])) {
array_push($stack, $map[$s[$i]]);
}else {
$pop = array_pop($stack);
if ($pop != $s[$i]) {
return false;
}
}
}
if (empty($stack)) {
return true;
}
return false;
}
网友评论