PHP-Leetcode-20

littlehero發表於2019-09-08
function isValid($s)
{
    $stack = new SplStack();
    $mapper = [
        '{' => "}",
        "[" => "]",
        "(" => ")",
    ];
    for ($i = 0; $i < strlen($s); $i++) {
        $v = $s[$i];
        if (in_array($v, ['(', '[', '{'])) {
           $stack->push($v);
        } else {
            if (!$stack->isEmpty()) {
                $peak = $stack->pop();
                if ($v != $mapper[$peak]) {
                    return false;
                } else {
                    continue;
                }
            } else {
                return false;
            }
        }
    }
    if ($stack->count() > 0) {
        return false;
    } else {
        return true;
    }
}

$ss = '([)]';
var_dump(isValid($ss));