stacks-queues · Problem 1 of 2

Valid Parentheses

easy
Amazon logoAmazon
Meta logoMeta
Google logoGoogle

Return true if every bracket in s is closed by the matching type in the right order.

isValid("()[]{}")  ->  true
isValid("(]")      ->  false

Your solution

Runs your code and animates it without grading anything. Change the input to see what it does on a case the tests do not cover.

Running is free — Submit is what records it. Or press ⌘↩

Tests

5 cases, 1 hidden
calltypeexpectedresult
isValid("()[]{}")all typestrue
isValid("(]")mismatchfalse
isValid("([{}])")nestedtrue
isValid("(")unclosedfalse
withheldhiddenwithheld

Hidden cases run too — their inputs aren't listed here, so aim for a general solution rather than one fitted to the cases above.

Complexity

target time
O(n)
target space
O(n) worst case

The stack holds at most every character, which happens when the string is all openers.

Hints

Stuck? Hints open one at a time, each giving a little more away.

3 hints left