Balanced Brackets
Given a string containing only the characters '(', ')', '{', '}', '[' and ']', determine if the input string is valid. A string is considered valid if all types of brackets are correctly closed and nested.
[ "()" ]
Explanation. Each open bracket has a corresponding close bracket of the same type.
[ "()[]{}" ]
Explanation. All brackets are closed and nested properly.
[ "(]" ]
Explanation. The bracket ')' does not have a corresponding opening '(' before it.
[ "([)]" ]
Explanation. Brackets are closed incorrectly.
[ "{[]}" ]
Explanation. All brackets are nested and closed correctly.
Follow-up: Can you solve the problem using a stack to monitor opening brackets?
The input string will only contain the characters '(', ')', '{', '}', '[' and ']', without any other characters or whitespace.
- Views
- 4