Balanced Parentheses Checker
Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the input string is valid. An input string is valid if:\n1. Open brackets must be closed by the same type of brackets.\n2. Open brackets must be closed in the correct order.\n3. Each close bracket has a corresponding open bracket of the same type.
[ "()" ]
Explanation. The parenthesis pair is matched correctly.
[ "()[]{}" ]
Explanation. All types of brackets are matched correctly and in the correct order.
[ "(]" ]
Explanation. The parentheses are not matched correctly; a closing square bracket cannot close an opening parenthesis.
[ "([)]" ]
Explanation. The brackets are not closed in the correct order.
[ "{[]}" ]
Explanation. All brackets are matched correctly and nested properly.
Follow-up: Consider cases with other characters or empty strings to check how your function handles unexpected input.
1. The string can include characters: '(', ')', '{', '}', '[' and ']' only.\n2. The length of the string will be between 1 and 10,000.
- Views
- 4