Given a string of balanced expression, find if it contains a redundant parenthesis or not. A set of parenthesis are redundant if the 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.
Note: The problem is intended to solve in O(1) extra space.
((a+b)) can reduced to (a+b)
(a+(b)/c) can reduced to (a+b/c) because b is surrounded by () which is redundant
(a+b*(c-d)) doesn’t have any redundant or multiple brackets
The idea is very similar to the idea discussed in the previous article but here in place of stack we are counting the symbol ( ‘+’, ‘*‘, ‘–‘ and ‘/‘ ) and the total number of brackets used in the expression.
If the count of brackets is not equal to the count of the symbols then the function will return false.
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.
- Expression contains redundant bracket or not
- Find index of closing bracket for a given opening bracket in an expression
- Check if the bracket sequence can be balanced with at most one change in the position of a bracket
- Minimum number of bracket reversals needed to make an expression balanced | Set - 2
- Minimum number of bracket reversals needed to make an expression balanced
- Check if a string contains only alphabets in Java using Lambda expression
- Count distinct regular bracket sequences which are not N periodic
- Check if a binary string has a 0 between 1s or not | Set 2 (Regular Expression Approach)
- Check if given Parentheses expression is balanced or not
- How to check string is alphanumeric or not using Regular Expression
- Check if an URL is valid or not using Regular Expression
- How to check Aadhar number is valid or not using Regular Expression
- Number of closing brackets needed to complete a regular bracket sequence
- Range Queries for Longest Correct Bracket Subsequence
- Minimum Cost required to generate a balanced Bracket Sequence
- Minimum Swaps for Bracket Balancing
- Print all ways to break a string in bracket form
- Convert an unbalanced bracket sequence to a balanced sequence
- Number of balanced bracket subsequence of length 2 and 4
- Print Bracket Number
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.