18. Valid Parentheses
MediumAlgorithms~15 min
Given a string containing only the characters ()[]{}, decide whether the brackets are valid.
Brackets are valid when every opening bracket is closed by the matching type, and they close in the correct order. An empty string is valid.
Examples
Example 1
- Input:
- str = "()[]{}"
- Output:
- true
Example 2
- Input:
- str = "(]"
- Output:
- false
- Why:
- A round bracket cannot be closed by a square one.
Example 3
- Input:
- str = "([)]"
- Output:
- false
- Why:
- The order of closing is wrong.
Constraints
0 <= str.length <= 1000The string contains only the six bracket characters.