i have written the code for redundant parentheses. it is showing WA. cam someone please tell me what is the error in my code or if my logic is incorrect where is my code failing.
What is the error in my code?
Your logic was incorrect , so I corrected your code https://ide.codingblocks.com/s/267440.
The correct logic is as follows.
The idea is to use stack. 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. 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. 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.
wasnβt my logic somewhat similar?
instead of using the same stack i used 2 different stacks ( 1 for the ( bracket and 1 for the ( other characters )
i stored the length of the subexpression in the other stack so that i can directly check if length is > 0 or not
could you please check my code once
the logic is atmost same
almost**************
using length of subexpression wonβt tell you that this subexpression was inside of the current braces or outside of current braces and hence your code may give you wrong answer in some cases.
run your code for this test case : 1 (1()+2), here you can see inside braces are redundant but your length vector has length of β1β hence your code will not take it as redundant.
Okay now I got it, thanks for the help
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.