revise Balanced Parentheses to general case