Using stack so resolve the issue.

If different parenthesis not considered, no stack would be needed.