Balanced Parentheses
Given a string containing only three types of characters: '(', ')' and '', write a function to check whether this string is valid. The string is valid if all the open parentheses are closed by the corresponding closing parenthesis and vice versa, and all '' can be treated as a single right parenthesis ')' or a single left parenthesis '(' or an empty string.
[ "()" ]
Explanation. The parentheses are balanced.
[ "(*)" ]
Explanation. The '*' can be treated as an empty string.
[ "(*))" ]
Explanation. The '*' can be treated as '('.
[ "(" ]
Explanation. There isn't a corresponding closing parenthesis.
[ "*(" ]
Explanation. '*' cannot be transformed to balance the parentheses.
Follow-up: Could you solve it without using extra space for another data structure (e.g., without explicitly using a stack or recursively calling a function)?
1. The string size will be in the range [1, 100].\n2. The string will contain only '(', ')', and '*' characters.
- Views
- 3