Practice
JavaScriptData StructuresReactConcepts
Sign in
← Back to problems

Valid Parentheses

Stackseasy

Implement `isValid(s)` that returns `true` if every opening bracket `(`, `[`, `{` in string `s` is closed by the correct closing bracket in the correct order. Example: `isValid("()[]{}")` → `true`, `isValid("(]")` → `false`.

Sample tests

Input: isValid("()")
Output: true
Input: isValid("()[]{}")
Output: true
Input: isValid("(]")
Output: false

+ 2 hidden tests run on Submit.

Hints

Common pitfalls
  • Forgetting to require an empty stack at the end accepts unclosed brackets.
  • Matching only by count ignores bracket type mismatches.

Learning resources

  • MDN: Array.prototype.push / pop
Approach & explanation (try first)

Push openers, pop and check the matching type on closers, and require an empty stack at the end. O(n) time and space.

Loading...
⌘/Ctrl + Enter

Run your code to see results.