![]() |
VOOZH | about |
Given a balanced expression string s, check if it contains redundant parentheses. Return true if redundant, else false.
Redundant Parentheses: Parentheses are redundant if removing them does not change the expression.
Note: Expression is valid, contains operators +, -, *, /, and no spaces.
Examples:
Input: s = "((a+b))"
Output: true
Explanation: ((a+b)) can be simplified to (a+b), which means the outer parentheses are redundant.Input: s = "(a+(b)/c)"
Output: true
Explanation: (a+(b)/c) can reduced to (a+b/c) because b is surrounded by () which is redundant.Input: s = "((a+b)*c)"
Output: false
Explanation: Removing any parentheses would change the order of evaluation, so none of them are redundant.
Redundant parentheses occur in two cases:
The idea is to use a stack so that whenever we encounter a closing parenthesis ')', we can check the element before it. For every such pair, if no operator (+, -, *, /) exists within, then the parentheses are redundant.
(' is found.true