Valid Parentheses
Given a string s containing only the characters '(', ')', '{', '}', '[' and ']', decide whether the string is valid. A string is valid when every opening bracket is closed by the same type of bracket, brackets close in the correct order, and every closing bracket has a matching opening bracket.
Open official problem prompt ↗Decide whether every bracket in the string is properly opened and closed in the correct nested order.
Think of a stack of plates: you can only remove the top plate. The last bracket you open is the first one you are allowed to close, exactly like the top plate.
- Input
- s = "()[]{}"
- Output
- true
- Why
- Each closing bracket immediately matches the most recent unmatched opening bracket of the same type, and nothing is left over.
1 <= s.length <= 10^4s consists only of the characters '()[]{}'