Balance The Brackets
Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the input string is valid. An input string is valid if:
- Open brackets must be closed by the same type of brackets.
- Open brackets must be closed in the correct order.
- Every close bracket has a corresponding open bracket of the same type.
[ "([)]" ]
Explanation. The brackets are not closed in the correct order.
[ "()" ]
Explanation. The brackets are correctly paired and ordered.
[ "()[]{}" ]
Explanation. All types of brackets are correctly paired and ordered.
[ "(]" ]
Explanation. The round bracket is incorrectly closed by a square bracket.
[ "{[]}" ]
Explanation. All types of brackets are correctly paired and nested.
[ "[" ]
Explanation. There is no corresponding closing bracket.
Follow-up: Can you optimize your solution to run in O(n) time complexity?
The input string will only contain the characters '(', ')', '{', '}', '[' and ']', and can be empty.
- Views
- 1