Given a string of balanced expression, find if it contains a redundant parenthesis or not. A set of parenthesis are redundant if same sub-expression is surrounded by unnecessary or multiple brackets. Print ‘Yes’ if redundant else ‘No’.
Note: Expression may contain ‘+‘, ‘*‘, ‘–‘ and ‘/‘ operators. Given expression is valid and there are no white spaces present.
Input: ((a+b)) (a+(b)/c) (a+b*(c-d)) Output: Yes Yes No Explanation: 1. ((a+b)) can reduced to (a+b), this Redundant 2. (a+(b)/c) can reduced to (a+b/c) because b is surrounded by () which is redundant. 3. (a+b*(c-d)) doesn't have any redundant or multiple brackets.
The idea is to use stack which is discussed in this article. For any sub-expression of expression, if we able to pick any sub-expression of expression surrounded by (), then we again left with () as part of string, we have redundant braces.
We 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, we push it to the stack. If the character is close parenthesis ‘)’, then pop characters from the stack till matching open parenthesis ‘(‘ is found.
Now for redundancy two condition will arise while popping-
- If immediate pop hits a open parenthesis ‘(‘, then we have found a duplicate parenthesis. For example, (((a+b))+c) has duplicate brackets around a+b. When we reach second “)” after a+b, we have “((” in the stack. Since the top of stack is a opening bracket, we conclude that there are duplicate brackets.
- If immediate pop doesn’t hit any operand(‘*’, ‘+’, ‘/’, ‘-‘) then it indicates the presence of unwanted brackets surrounded by expression. For instance, (a)+b contain unwanted () around a thus it is redundant.
Yes Yes No
Time complexity: O(n)
Auxiliary space: O(n)
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.
- Check if expression contains redundant bracket or not | Set 2
- Find index of closing bracket for a given opening bracket in an expression
- Minimum number of bracket reversals needed to make an expression balanced
- Minimum number of bracket reversals needed to make an expression balanced | Set - 2
- Check if the bracket sequence can be balanced with at most one change in the position of a bracket
- Check if the bracket sequence can be balanced with at most one change in the position of a bracket | Set 2
- Building Expression tree from Prefix Expression
- Print Bracket Number
- Minimum Swaps for Bracket Balancing
- Number of balanced bracket subsequence of length 2 and 4
- Construct Binary Tree from String with bracket representation
- Range Queries for Longest Correct Bracket Subsequence Set | 2
- Range Queries for Longest Correct Bracket Subsequence
- Print all ways to break a string in bracket form
- Number of closing brackets needed to complete a regular bracket sequence
- Convert an unbalanced bracket sequence to a balanced sequence
- Expression Evaluation
- What is an Expression and What are the types of Expressions?
- Balanced expression with replacement
- Arithmetic Expression Evalution
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.