Update answer by adding minimum of removing both unbalanced open and closed brackets or inserting closed unbalanced brackets to end of String
result += Math . min ( a * ( openUnbalanced + closedUnbalanced ) , b * closedUnbalanced ) ;