Remove unnecessary parentheses You are given a string composed with the characters 0123456789+*(). You can assume that string is always a valid mathematical expression.

Your task is to remove the unnecessary parentheses, assuming multiplication has higher priority than addition.

The parentheses should be removed only when they are not needed structurally:


because of multiplication higher priority: 3+(4*5) => 3+4*5
because of multiplication or addition associativity: 3*(4*5) => 3*4*5
when they are redundant around an expression: 3*((4+5)) => 3*(4+5)


Parentheses should be kept when they could be simplified because of specific number values:


1*(2+3) should not be simplified to 1*2+3
0*(1+0) should not be simplified to 0*1+0


Examples:

(4*12)+11         ==&gt;    4*12+11
(1+2)*3           ==&gt;    (1+2)*3
3*(4*5)           ==&gt;    3*4*5
((((523))))       ==&gt;    523
(1+1)             ==&gt;    1+1
1*(2*(3+4)*5)*6   ==&gt;    1*2*(3+4)*5*6
1*(2+3)           ==&gt;    1*(2+3)
0*(1+0)           ==&gt;    0*(1+0)


(((2+92+82)*46*70*(24*62)+(94+25))+6)    ==&gt;    (2+92+82)*46*70*24*62+94+25+6
Mathematica, 105 97 91 bytes

-6 bytes thanks to Roman!

a=StringReplace;ToString@ToExpression@a[#,{"*"-&gt;"**","+"-&gt;"~~"}]~a~{" ** "-&gt;"*","~~"-&gt;"+"}&amp;


Replaces + and * with ~~ (StringExpression) and ** (NonCommutativeMultiply) respectively, evaluates it, stringifies it, and replaces the operators back.