Balanced Parentheses
Given a string containing just the characters (, ), {, }, [ and ], determine if the input string is valid. An input string is valid if:
- Open brackets must be closed by the same type of brackets.
- Open brackets must be closed in the correct order.
- An empty string is also considered valid.
[ "()" ]
Explanation. This standard pairing is valid.
[ "()[]{}`" ]
Explanation. All types of brackets are correctly nested and closed in the right order.
[ "(]" ]
Explanation. The parenthesis is closed with a different type of bracket.
[ "([)]" ]
Explanation. The brackets are not closed in the correct order.
[ "{" ]
Explanation. The single opening brace is never closed.
[ "{[]}" ]
Explanation. Complex nesting is valid and closed correctly.
Follow-up: Could there be any performance considerations if the function is to be run against a large dataset?
The length of the input string is between 0 and 10,000. You may assume the string contains only the characters listed above.
- Views
- 4