InterviewBit Academy is now Scaler!
InterviewBit Academy is now Scaler Academy!

Minimum Bracket Reversals


Problem Description

Given a string A consisting only of '(' and ')'.

You need to find minimum number of bracket reversals needed to make the expression balanced, if it not possible to make string balanced then return -1.



Problem Constraints

1 <= |A| <= 105



Input Format

First argument is an string A.



Output Format

Return a single integer denoting the minimum number of bracket reversals required to make the expression balanced, if it not possible to make string balanced then return -1.



Example Input

Input 1:

 A = "()"

Input 2:

 A = "((("



Example Output

Output 1:

 2

Output 2:

 -1



Example Explanation

Explanation 1:

 We need to change ')' to '(' and '(' to ')' so that the expression becomes balanced, the balanced expression is '()'

Explanation 2:

 It is not possible to make the string balanced by any number of bracket reversals.



NOTE: You only need to implement the given function. Do not read input, instead use the arguments to the function. Do not print the output, instead return values as specified. Still have a doubt? Checkout Sample Codes for more details.
Start solving Minimum Bracket Reversals on Interview Code Editor
Sign Up
to access hints and editorial solutions for Minimum Bracket Reversals
Asked In:

Discussion


Loading...
Click here to start solving coding interview questions