Challenge 8: Check Balanced Parentheses Using Stack
If you are given an expression, can you check if its parentheses are balanced?
We'll cover the following
Problem statement
In this problem, you have to implement the isBalanced()
function, which will take a string containing only curly {}
, square []
, and round ()
parentheses. The function will tell you 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.
Input
The input is 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
string exp = "{[({})]}";
Sample output
True
Level up your interview prep. Join Educative to access 80+ hands-on prep courses.