Check Balance Brackets
Given a string consisting only of the characters '(', ')', '{', '}', '[' and ']', determine if the input string is valid and correctly balanced. A string is considered balanced if each type of bracket is closed and opened in the correct order such that every opening bracket has a matching closing bracket of the same type and they are correctly nested within each other.
[ "()" ]
Explanation. There is one pair of parenthesis which matches correctly.
[ "()[]{}" ]
Explanation. All types of brackets open and close correctly and are well nested.
[ "(]" ]
Explanation. A parenthesis starts but is incorrectly closed by a square bracket.
[ "([)]" ]
Explanation. Brackets are not properly nested inside one another.
[ "{[]}" ]
Explanation. All brackets open and close correctly and are properly nested.
Follow-up: Could this solution be optimized for a real-time text editor integration for checking balance in coding scripts?
1. The length of the string is at least 1 and at most 1000.\n2. The string consists only of the characters '()', '{}', and '[]'.
- Views
- 4