Duplicate Parentheses Problem
● You all remember the “Matching Parentheses” problem from Stacks. Don’t you?
● Well here’s another problem statement involving those parentheses again.
● For any programming language structure or for any algebraic expression, it is
mandatory to have all parenthesis “( )” balanced.
● However, there may be instances where these parentheses are duplicated.For
example: (((1*2))+5)
● Let’s see the approach now:
Duplicate Parentheses Problem
● If you had to solve this problem manually, wou will definitely start by resolving the
innermost parentheses. Our algorithm works on the same principle.
● We start by declaring a stack and iterating through the complete length of string
and pushing the elements inside the stack until we encounter the first closing
parentheses “)”.
Duplicate Parentheses Problem
● Now we start popping the elements till we encounter the first “(”.
● If the count of number of elements popped is 1 or 0, we say that string contains
duplicate parentheses.
● Otherwise, after full string traversal, we say that it does not contains duplicate
parentheses.