Solution Review: Reverse Parentheses
Let's discuss in detail the solution for the reverse parentheses problem.
We'll cover the following...
We'll cover the following...
Solution
-
We don’t need to flip the balanced parentheses, so we remove them first. If all the balanced parentheses are removed, we’ll be left with the parentheses of the form
)))((((. -
Let’s name the total number of open parenthesis as
OpenCountand the total number of closed parenthesis asCloseCount. -
When
OpenCountis even, thenCloseCountis also even. Their half-element reversal will make the expression balanced. -
When
OpenCountis odd andCloseCountiis also odd, we have to perform OpenCount/2 and CloseCount/2 reversals. We’ll be left with )(, which needs 2 more reversals. The formula is derived from this: