Incorrect answer in Redundant Paranthesis

Here is my code,
#include
#include
#include

using namespace std;

bool redPar(string str){
stack st;
bool flag = false;
for(int i =0; i< str.length(); i++){
char ch = str.at(i);
if(ch != ‘)’){
st.push(ch);
}else{
int counter = 0;
while(st.top()!=’(’){
st.pop();
counter++;
}
st.pop();
// cout<<counter<<"\n";
if(counter== 0){
flag = true;
return flag;
}
}
}
return flag;
}

int main() {

int t; cin>>t;


for(int i =0; i<t; i++){
string str;
getline(cin,str);

bool flag =  redPar(str);

if(flag) cout<<"Duplicate\n";
else cout<<"Not Duplicates\n";
}
return 0;

}

@ap8730390 when using getline(cin, s) after cin it is reading the blank space after the integer.
u can use cin for the string .refer to this thread

Thank You So Much for Doubt Clarification

1 Like