Balanced String Checker
Given a string consisting only of the characters (, ), {, }, [, and ], determine if the string is valid.
A string is considered valid if all the following conditions are met:
- Open brackets are closed by the same type of brackets.
- Open brackets are closed in the correct order.
- Every close bracket has a corresponding open bracket of the same type.
[ "()" ]
Explanation. Single type of matching parentheses.
[ "()[]{}" ]
Explanation. Multiple types of matching brackets, all closed in the correct order.
[ "(]" ]
Explanation. The parenthesis is closed with a square bracket, which is incorrect.
[ "([)]" ]
Explanation. Brackets are not closed in the correct order.
[ "{[]}" ]
Explanation. Complex nesting of all bracket types that are closed correctly.
Follow-up: Can you extend your solution to handle other types of brackets or symbols, including nested structures like HTML tags?
The input string will only contain the characters '(', ')', '{', '}', '[' and ']'.
- Views
- 3