Code does not pass test cases
Hi kopal, Your seem to have implemented the wrong logic. should i tell you the logic or give u a hint. ??
things which are wrong in your code:
-
You need to only check for brackets not any other character that u are pushing in the stack. That is wrong
-
Second your code does not work properly for some cases.
if I only push and check for brackets, expressions like (a) would give a wrong answer. The logic I thought was that there should be an operator in between the two brackets. can you give me a hint where my code is failing please
Oh ok i got ur logic seems like i was understanding it wrong.
Please wait i will while i check it.
Thank u
Hi Your code was correct…But i think there was just a tiny mistake…
I corrected it … It seems like either u were missing any operand or something like that … i just rewrote that condition and voila!! it worked.
#include<bits/stdc++.h>
using namespace std;
#define ll long long
int main(){
int t;
cin>>t;
while(t--){
string str;
cin>>str;
stack<char>s;
int flag=0;
for(int i=0;i<str.length();i++){
if(s.empty() or str[i]!=')')
{
s.push(str[i]);
}
else{
if(s.top()=='(') { flag=1; break;}
while(!s.empty() and s.top()!='(') s.pop();
s.pop();
}
}
if(flag==1) cout<<"Duplicate"<<endl;
else cout<<"Not Duplicates"<<endl;
}
}
I got it, thanks. Was probably missing some operator
Glad to be helpful . 