Valid Parentheses
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
trueif every bracket ofsis closed by a bracket of its own kind, and brackets close in the reverse order they opened; otherwisefalse.- constraints
- 1 ≤ s.length ≤ 10⁴
- s: only the brackets ()[]{}
- 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
Stuck?
See the solution run step by step on our example, then why it works and its complexity.
Next: more stack problems
The problem and its statement belong to LeetCode. Here are our own retelling, example and solution. Patterns Hero is not affiliated with LeetCode.