Balanced Parentheses Checker
Develop a function that checks if a given string containing just the characters (', )', '{', '}', '[', ']' is valid. A string is valid if all types of brackets open and close correctly and are nested correctly. For example, "()" and "()[]{}" are valid, but "(]" and "([)]" are not.
[ "()" ]
Explanation. The parentheses are balanced and correctly nested.
[ "()[]{}" ]
Explanation. All types of brackets are balanced and correctly nested.
[ "(]" ]
Explanation. The parentheses close with a different type, making it unbalanced.
[ "([)]" ]
Explanation. The brackets are not nested correctly.
[ "{[]}" ]
Explanation. The brackets are nested correctly and all types are balanced.
[ "((((((" ]
Explanation. There are no corresponding closing brackets.
Follow-up: Can your solution handle complex nested structures efficiently? How does it perform with strings near the maximum length limit?
The input string will only contain the characters: `(`, `)`, `{`, `}`, `[`, `]`. The length of the string will be at most 10000 characters.
- Views
- 3