Redundant parenthesis problem

How to approach this problem, can you please suggest

@AbhishekAhlawat1102,

The idea is to use stack.

  1. 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, push it to the top of the stack.
  2. If the character is close parenthesis ‘)’, then pop characters from the stack till matching open parenthesis ‘(‘ is found and a counter is used, whose value is incremented for every character encountered till the opening parenthesis ‘(‘ is found.
  3. If the number of characters encountered between the opening and closing parenthesis pair, which is equal to the value of the counter, is less than 1, then a pair of duplicate parenthesis is found else there is no occurrence of redundant parenthesis pairs.

For example, (((a+b))+c) has duplicate brackets around “a+b”. When the second “)” after a+b is encountered, the stack contains “((“. Since the top of stack is a opening bracket, it can be concluded that there are duplicate brackets.

Hope, this would help.

https://ide.geeksforgeeks.org/o9xcMaU1ta-- i have implemented the approach u suggested but the output is not correct, can you please tell what i m doing wrong ?

@AbhishekAhlawat1102,
Are you sure you have shared the right code? This looks like the code for another question you just posted.

ya, this the code i have written for the redundant parenthesis problem—https://ide.geeksforgeeks.org/o9xcMaU1ta

plzz check this link

@AbhishekAhlawat1102,
https://ide.codingblocks.com/s/271205 corrected code.

I have added explanation on the changes in the code itself.

Also you have to print “Not Duplicates” instead of “No Duplicates”

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.