Challenge: Check for Balanced Parentheses Using a Stack

If you are given an expression, can you check if its parentheses are balanced? A solution is placed in the "solution" section to help you, but we would suggest you try to solve it on your own first.

Problem Statement #

In this problem, you have to implement the isBalanced() method, which will take a string containing only curly {}, square [], and round () parentheses. The method will tell us whether all the parentheses in the string are balanced or not.

For all the parentheses to be balanced, every opening parenthesis must have a closing one. The order in which they appear, also matters. For example, {[]} is balanced, but {[}] is not.

Method Prototype: #

boolean isBalanced(String exp);

where exp is an expression containing a combination of parentheses.

Input #

A string consisting solely of (, ), {, }, [, and ]

Output #

It returns False if the expression doesn’t have balanced parentheses. If it does, the function returns True.

Sample Input #

exp = "{[({})]}"

Sample Output #


Level up your interview prep. Join Educative to access 70+ hands-on prep courses.