Given a balanced expression, find if it contains duplicate parenthesis or not. A set of parenthesis are duplicate if the same subexpression is surrounded by multiple parenthesis.
Below expressions have duplicate parenthesis - ((a+b)+((c+d))) The subexpression "c+d" is surrounded by two pairs of brackets. (((a+(b)))+(c+d)) The subexpression "a+(b)" is surrounded by two pairs of brackets. (((a+(b))+c+d)) The whole expression is surrounded by two pairs of brackets. Below expressions don't have any duplicate parenthesis - ((a+b)+(c+d)) No subsexpression is surrounded by duplicate brackets. ((a+(b))+(c+d)) No subsexpression is surrounded by duplicate brackets.
It may be assumed that the given expression is valid and there are not any white spaces present.
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.
Below is the implementation of above idea :
Time complexity of above solution is O(n).
Auxiliary space used by the program is O(n).
This article is contributed by Aditya Goel. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
- Check for balanced parentheses in an expression
- Expression Evaluation
- Find maximum depth of nested parenthesis in a string
- Find the nearest smaller numbers on left side in an array
- Find maximum of minimum for every window size in a given array
- Minimum number of bracket reversals needed to make an expression balanced
- Find maximum difference between nearest left and right smaller elements
- Find next Smaller of next Greater in an array
- Find maximum sum possible equal sum of three stacks
- Stack | Set 4 (Evaluation of Postfix Expression)
- Iterative method to find ancestors of a given binary tree
- Find index of closing bracket for a given opening bracket in an expression
- Identify and mark unmatched parenthesis in an expression
- Expression contains redundant bracket or not
- Arithmetic Expression Evalution