Leetcode doubt o sme lines

class Solution {
public:
string removeOuterParentheses(string s) {
int n = s.size();
if(n<2)
return β€œβ€;
string out;
stack st;
int balance =0;
int starting =0;
for(int i =0;i<n;i++){

    if(s[i]=='(')
        st.push(s[i]);
    else{
        if(st.size()>1)
            st.pop();
        else
        {
            if(!st.empty()){
                st.pop();
            balance = i;
            out += s.substr(starting+1,balance-starting-1);
           // cout<<balance<<" "<<starting<<endl;
            starting = i+1;
            //cout<<balance<<" "<<starting<<endl;
            }
        }
        
        
            
    }
}

return out;

}
};

In this code I cannot understand the

//balance = i;
out += s.substr(starting+1,balance-starting-1);
// cout<<balance<<" β€œ<<starting<<endl;
starting = i+1;
//cout<<balance<<” "<<starting<<endl;
//

Please help me here

hello @Somasree

in which line u have doubt?

balance = i;
out += s.substr(starting+1,balance-starting-1);
// cout<<balance<<" β€œ<<starting<<endl;
starting = i+1;

In these lines

in starting variable they are storing starting index of the pattern (Ie from where the new pattern starts)

balance is variable that holds ending index of pattern (ie where the pattern ends)

in this line they are adding [staring+1…balance-1] part of given string to their answer (ie out string) . { starting +1 becuase we have ( at index starting we want to remove it , similarly balance-1 becuase we have ) at index balance and we want to remove it).

and becuase pattern ends here , new pattern will start from index i+1

thats why we have this line.

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.