Wrong Answer in Challenge

I think my algo’s all right and it accounts for all cases if I have understood the problem correctly. Please help.

Hey can you post your code?

https://codeshare.io/adQV0B

the CB ide is not showing any option to save and share, so I am sharing through Codeshare

did you find any mistake?

Hey, your code is logically wrong, Refer to this and try to write it correctly:-
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 or equal to 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.

I think that is exactly what I’ve done. I’ve made a stack in which I push the characters and opening brackets. When a closing bracket is encountered, I check the top of the stack. If it’s anything other than (, I pop it to the nearest ( bracket. Otherwise if the top of stack is (, it means other characters have already been popped and this is a redundant bracket. I’ve checked it on some sample cases too. can you give an example where my code would give wrong answer?

please send your code above link doesn’t contain code

save your code at


click on file->save
and then send the link generated