How to approach this question.
Please Just give a small hint donβt give full algorithm
Redundant parenthesis
We iterate through the given expression and for each character in the expression, if the character is a open parenthesis β(β or any of the operators or operands, we push it to the stack. If the character is close parenthesis β)β, then pop characters from the stack till matching open parenthesis β(β is found.
Now for redundancy two condition will arise while popping-
-
If immediate pop hits a open parenthesis β(β, then we have found a duplicate parenthesis. For example, (((a+b))+c) has duplicate brackets around a+b. When we reach second β)β after a+b, we have β((β in the stack. Since the top of stack is a opening bracket, we conclude that there are duplicate brackets.
-
If immediate pop doesnβt hit any operand(β*β, β+β, β/β, β-β) then it indicates the presence of unwanted brackets surrounded by expression. For instance, (a)+b contain unwanted () around a thus it is redundant.