recategorized by
1,316 views

2 Answers

1 votes
1 votes
We can have a basic parenthesis matching algorithm were we use a stack and match each opening and closing braces. Now, along with an opening brace we can also store its index and while matching the closing brace, we can find duplicates if the matches are consecutive. i.e.; if the closing braces are consecutive and the indices of their matching opening braces are consecutive, then they are duplicates.
0 votes
0 votes

Process expression from left to right and do following operations: 

1.If it is not a closing parenthesis '(' 
     push it to the stack 
2.Else if it is a closing parenthesis ')
   
 If top of the stack is an opening parenthesis '(', you found the duplicate.  
     otherwise, pop everything till you get an opening parenthesis '(' and pop that opening parenthesis '(' as well.

have a look at code and more explanation here http://www.waytocrack.com/forum/627/check-if-there-are-duplicate-parenthesis-in-an-expression 
If you want indexes of these parentheses, store index information also in the stack along with characters.

Related questions

0 votes
0 votes
1 answer
1
iarnav asked May 16, 2017
2,129 views
What does that word item means in DS subject especially in stacks. is item a variable?Kindly explain, thanks!
1 votes
1 votes
1 answer
3
2 votes
2 votes
1 answer
4
shekhar chauhan asked Jul 14, 2016
1,740 views
If we have to construct the expression tree from this expression (3 + ((5+9)*2)) then what will be the order of push and pop operation on a stack ?Explain what to do with...