Remove redundant parentheses from an arithmetic expression
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.
Introduction
In mathematical and computer programming contexts, parentheses play a crucial role in determining the order of operations. However, redundancy in parentheses can make arithmetic expressions unnecessarily complex and difficult to comprehend. Removing these redundant parentheses not only simplifies the expression but also enhances readability and maintainability, especially in large codebases or complex algorithms.
Understanding Parentheses in Arithmetic Expressions
Parentheses are used in arithmetic expressions to alter the natural precedence of operations. For instance, in the expression (2 + 3) * 4
, parentheses ensure that addition precedes multiplication. However, in some cases, parentheses are redundant and can be safely removed without changing the expression's outcome. Consider the expression 2 * (3 + 4)
. Here, removing parentheses would change the expression's meaning, so they are necessary. Conversely, parentheses around the entire expression (2 * 3) * 4
could be removed, as the multiplication operation's precedence would dictate the same evaluation order.
Identifying Redundant Parentheses
To determine the redundancy of parentheses in an arithmetic expression, several approaches can be employed, including parsing techniques and algorithmic strategies. Below, we illustrate a step-by-step method to identify and remove redundant parentheses:
- Operator Precedence: Understand the precedence of arithmetic operators. Typically, operators like multiplication and division have higher precedence than addition and subtraction. Parentheses can be deemed redundant if removing them does not alter the precedence of operations in the expression.
- Parentheses Matching: Ensure each opening parenthesis has a corresponding closing parenthesis. Not only does this ensure syntactic correctness, but it also aids in identifying unnecessary nested parentheses.
- Simplification Rules: Apply simplification rules such as: • If a parenthesis encloses an entire expression without altering precedence, it is redundant. For example,
(a + b)can be simplified toa + b. - Traversal Algorithms: Utilize stack-based algorithms to traverse the expression. Stack data structures are particularly useful for balancing parentheses and can help quickly identify redundant parentheses through depth tracking.
Example: Redundant Parentheses Removal
Let's take an example to illustrate removing redundant parentheses:
- Initial Check: Identify parentheses groups and their nesting levels.
- Evaluate Redundancy: • The outermost parentheses around
((a + b) + ((c)))could be removed, but first, examine inner expressions. • The parentheses around((c))are redundant since there's no precedence conflict or operational necessity, simplifying toc. • After removing these parentheses, the expression becomes(a + b) + c. - Final Simplification: As addition is associative, the final expression
a + b + cis equivalent to the initial expression.
Code Implementation
Below is a Python function that removes redundant parentheses from a given arithmetic expression. This function assumes that the input is a well-formed mathematical expression.
Related reading
- Remove substrings inside a list with better than On2 complexity
- Remove the minimum number of blades
- Removing almost duplicate strings in subquadratic time
- Removing duplicates in lists
- Remove unused references using
- Removing duplicate elements from an array in Swift
- Removing duplicates in lists
- Reorder a string by half the character

DSA Fundamentals
Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.