can u plz drop any hint to approch this prob means for what condition we have to look in the stack ,
is that it when we get simultaneously two )) and pop out twice???
Redundant parentheses
idea is to use stack here
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.
function to check redundant paranthesis
bool checkRedundancy(string& str)
{
// create a stack of characters
stack<char> st;
// Iterate through the given expression
for (int i=0;str[i]!='\0'; i++) {
// if current character is close parenthesis ')'
if (ch == ')') {
char top = st.top();
st.pop();
// If immediate pop have open parenthesis '('
// duplicate brackets found
bool flag = true;
while (top != '(') {
// Check for operators in expression
if (top == '+' || top == '-' ||
top == '*' || top == '/')
flag = false;
// Fetch top element of stack
top = st.top();
st.pop();
}
// If operators not found
if (flag == true)
return true;
}
else
st.push(ch); // push open parenthesis '(',
// operators and operands to stack
}
return false;
}
if you have more doubts regarding this feel free to ask
i hope this helps
if yes hit a like
: and donβt forgot to mark doubt as resolved 
sir ye jo 2 condition di hui hai vo to smz aa rhi hai lekin code me thodi problem hai like if we get ch==β)β then why did we do poping first without checking any cond??
can u plz make it more understandable to meβ¦
if we encounter β)β then we have to definitely pop() but to check the condition we are storing the top element of stack in variable βtopβ
done but sir jo que sir ne lec me krvaye hai unke alawa mujhe idea nhi ki kahan pe stack queue use kr skte hai ,isme confidence build nhi ho rha so can u plz help me with thisβ¦
practice makes the man perfect
try to solve question on your own
do something even wrong no matter
if you done wrong then only you can understand the correct approach and donβt do that mistake again