Valid Parentheses

Easy#20brackets

You get a string of the brackets ( ) [ ] { }. It is valid when every bracket is closed by one of its own kind and the pairs never cross: what opened last closes first. Our example, two calls: "{[()()]}" gives true and "(()[]" gives false, the first "(" is never closed.

input
s: a string of the brackets (, ), [, ], { and }.
returns
true if every bracket of s is closed by a bracket of its own kind, and brackets close in the reverse order they opened; otherwise false.
constraints
  • 1 ≤ s.length ≤ 10⁴
  • s: only the brackets ()[]{}
our 17 tests keep to them
examples
Open a test in the tests panel: it shows the input and the expected answer.
/**
 * @param {string} s
 * @return {boolean}
 */
function isValid(s) {

}
Ctrl ↵
format
reset

saved in this browser

17 tests · limit 3 s

Stuck?

See the solution run step by step on our example, then why it works and its complexity.

Watch the walkthrough →

Next: more stack problems

Pattern “Stack” →

The problem and its statement belong to LeetCode. Here are our own retelling, example and solution. Patterns Hero is not affiliated with LeetCode.