Balanced Delimiters
Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the input string is valid. An input string is valid if:
- Open brackets are closed by the same type of brackets.
- Open brackets are closed in the correct order.
- Every close bracket has a corresponding open bracket of the same type.
[ "()" ]
Explanation. The parentheses are balanced.
[ "()[]{}" ]
Explanation. All types of brackets are correctly balanced.
[ "(]" ]
Explanation. The parentheses are closed in the incorrect order.
[ "([)]" ]
Explanation. The brackets are not closed in the correct order.
[ "{[]}" ]
Explanation. All types of brackets are balanced and closed correctly.
[ "" ]
Explanation. An empty string is trivially balanced.
Follow-up: Can you extend the solution to include other types of brackets or delimiters?
The input string will only contain the characters '(', ')', '{', '}', '[' and ']', and can be empty.
- Views
- 13