Valid Parentheses
You are given a string s that contains only the bracket characters (, ), [, ], { and }.
Decide whether the brackets are balanced:
- every opening bracket is eventually closed by a bracket of the same type,
- brackets close in the reverse order they were opened, so an inner pair closes before the pair around it, and
- no closing bracket appears without an open bracket waiting for it.
Return true if s is balanced and false otherwise. An empty string is balanced.
Constraints
0 <= s.length <= 10^4sconsists only of the characters()[]{}.
Examples
Example 1
Input: s = "()[]{}"
Output: trueEach pair opens and closes immediately, one after another.
Example 2
Input: s = "([)]" Output: false
When ] arrives, the most recently opened bracket is (, not [. The brackets cross instead of nesting.
Example 3
Input: s = "{[]}"
Output: true[] closes inside {}, and then {} closes: properly nested.
JavaScript you'll use: Stacks, queues and deques Map and Set
/**
* @param {string} s
* @return {boolean}
*/
function isValid(s) {
}
Ctrl/⌘ + ' runs the examples, Ctrl/⌘ + Enter submits. Use console.log freely: output shows under Console. In the editor, press Esc then Tab to move focus out.