stacks-queues · Problem 1 of 2
Valid Parentheses
easy
Amazon
Meta
Google
Return true if every bracket in s is closed by the matching type in the right order.
isValid("()[]{}") -> true
isValid("(]") -> falseYour 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| call | type | expected | result |
|---|---|---|---|
| isValid("()[]{}") | all types | true | — |
| isValid("(]") | mismatch | false | — |
| isValid("([{}])") | nested | true | — |
| isValid("(") | unclosed | false | — |
| withheld | hidden | withheld | — |
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