Count the Reversals | Geeks for Geeks Solution

 Count the Reversals | Geeks for Geeks Solution

Given a string S consisting of only opening and closing curly brackets '{' and '}', find out the minimum number of reversals required to convert the string into a balanced expression.
A reversal means changing '{' to '}' or vice-versa.

Example 1:

S = "}{{}}{{{"
Output: 3
Explanation: One way to balance is:
"{{{}}{}}". There is no balanced sequence
that can be formed in lesser reversals.

รข€‹Example 2:

S = "{{}{{{}{{}}{{"
Output: -1
Explanation: There's no way we can balance
this sequence of braces.

Your Task:
You don't need to read input or print anything. Your task is to complete the function countRev() which takes the string S as input parameter and returns the minimum number of reversals required to balance the bracket sequence. If balancing is not possible, return -1. 

Expected Time Complexity: O(|S|).
Expected Auxiliary Space: O(1).

1 ≤ |S| ≤ 105

int countRev (string s)
    if(s.length()%2==1)return -1;
    int cnt=0;
    int ans=0;
    for(auto c:s){
        else if(c=='}' && !st.empty())st.pop();
        else {
    if(st.size()%2!=0)return -1;
    return st.size()/2+ans;

Post a Comment