Live Batches
Masterclasses
Menu
Free Courses
Account
Login / Sign Up

Redundant Parenthesis

Hard Acceptance 51.49% Points 40.00

Given a valid expression containing only the binary operators '+', '-', '*', '/' and operands, remove all redundant parentheses. Parentheses are considered redundant if removing them does not affect the value of the expression.

Note: The operators '+' and '-' have the same precedence, as do '' and '/'. However, '' and '/' have higher precedence than '+' and '-'.

Your task is to complete the function removeBrackets() which takes the string Exp as input parameters and returns the updated expression.

Examples
Example 1

Example 1

Input: Exp = (A + (B - C))

Output: A + (B - C)

Explanation: The outer parentheses are redundant as removing them does not change the expression.

Example 2

Example 2

Input: Exp = ((A + B) * C)

Output: (A + B) * C

Explanation: The outer parentheses are redundant because the multiplication operation has higher precedence than addition, so the parentheses are unnecessary.

Hints
Hint 1
Expected Time Complexity: O(N)
Hint 2
Expected Auxiliary Space: O(N)
Constraints
  • 1 < Length of Exp < 10^5
  • Exp contains uppercase english letters, '(' , ')', '+', '-', '*' and '/'.
Companies
Oracle Paypal
Topics
String Stack
Solution.cs C#JavaPythonC++Javascript

Unlock the code editor

Sign in to write, run, and submit your solution against the full test suite.

  • Run code against sample & hidden test cases
  • Save submissions and track your streak
  • Compare with editorial & community solutions