Balanced Brackets
Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the input string is valid and all the brackets are balanced. An input string is balanced if:\n1. Open brackets must be closed by the same type of brackets.\n2. Open brackets must be closed in the correct order.\n3. An empty string is considered balanced.
[ "()" ]
Explanation. Single type of bracket pairs, properly closed.
[ "()[]{}" ]
Explanation. Multiple types of bracket pairs, all appropriately closed.
[ "(]" ]
Explanation. Mismatched bracket types.
[ "([)]" ]
Explanation. Brackets closed in the incorrect order.
[ "{[]}" ]
Explanation. Nested brackets properly closed.
Follow-up: Is there a way to optimize the solution using a different data structure?
1. The string may contain any of the characters: '(', ')', '{', '}', '[' and ']' only.\n2. The length of the string is at most 10^4.
- Views
- 2