59. Paired Brackets
A code editor checks whether the brackets in a line are properly paired. The string s contains only the six characters (, ), [, ], { and }. It is balanced when every opening bracket is closed by a bracket of the same kind, and brackets are closed in the correct order: the most recently opened bracket must be the next one closed.
Return true if s is balanced and false otherwise. A stack of the brackets that are still open is the natural tool.
Example 1
- Input:
- s = "{[()]}"
- Output:
- true
- Explanation:
Each bracket closes the most recent open one of its kind, so the line is balanced.
Example 2
- Input:
- s = "{[}]"
- Output:
- false
- Explanation:
The } arrives while [ is the most recent open bracket, so the order is wrong and the line is not balanced.
Constraints
1 ≤ s.length ≤ 104
s contains only the characters ()[]{}
How this problem is judged
- Answers
- Your answer must match exactly. Numbers compare by value, so 2 and 2.0 are equal.
Expected complexity
- Time
- O(n)
- Space
- O(n)