Easy to understand Time O(n) and space O(1)


#1

int Solution::solve(string A) {
int n=A.size(),ct=0,ct1=0;
for(int i=0;i<n;i++){
if(ct==0 && A[i]==’)’) ct1++;
else if(A[i]==’(’) ct++;
else ct–;
}
return ct+ct1;
}