Valid Parentheses
Easy · stack
You are given a string s made only of the characters (, ), [, ], { and }. Return True if the brackets are balanced, and False otherwise.
A string is balanced when every opening bracket is closed by a bracket of the same kind, and brackets are closed in the reverse order they were opened. The empty string is balanced.
Examples
Example 1
Input: s = "([]){}"
Output: True
Each bracket closes the most recent one that is still open.
Example 2
Input: s = "(]"
Output: False
The ( is still open when the ] arrives, and they are different kinds.
Example 3
Input: s = "(("
Output: False
Both brackets are opened and never closed.
Constraints
- 0 <= len(s) <= 10^4
- s contains only the characters ()[]{}