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.
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Find a valid parenthesis sequence of length K from a given valid parenthesis sequence
- Identify and mark unmatched parenthesis in an expression
- InfyTQ 2019 : Find the position from where the parenthesis is not balanced
- Find maximum depth of nested parenthesis in a string
- Check for balanced parenthesis without using stack
- Reverse substrings between each pair of parenthesis
- Calculate weight of parenthesis based on the given conditions
- Expression contains redundant bracket or not
- Find index of closing bracket for a given opening bracket in an expression
- Remove all duplicate adjacent characters from a string using Stack
- Check for balanced parentheses in an expression
- Expression Evaluation
- Minimum number of bracket reversals needed to make an expression balanced
- Stack | Set 4 (Evaluation of Postfix Expression)
- Arithmetic Expression Evalution
- Balanced expression with replacement
- Program to convert Infix notation to Expression Tree
- Convert ternary expression to Binary Tree using Stack
- Parsing String of symbols to Expression
- Solve the Logical Expression given by string