Balanced Parentheses
Given a string consisting of parentheses characters '(', ')', '{', '}', '[' and ']', determine if the input string is valid. A string is considered valid if all the brackets are opened and closed appropriately and nested correctly. For every opening bracket, there should be a corresponding matching closing bracket of the same type.
[ "()" ]
Explanation. Single type of balanced parentheses.
[ "()[]{}" ]
Explanation. Multiple types of balanced parentheses mixed but appropriately closed.
[ "(]" ]
Explanation. Parentheses are not matching.
[ "([)]" ]
Explanation. Brackets are closed in the wrong order.
[ "{[]}" ]
Explanation. Multiple brackets properly nested.
Follow-up: How would you handle different types of brackets, such as angle brackets '<' and '>'?
The input string will only contain the characters: '(', ')', '{', '}', '[' and ']'.
- Views
- 4