We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
The Input which does not works correctly in testcase 8 is:-
10
MY_MMM___
The Output must be NO but your program is giving YES.
This error is happening because loop breaks before flag equals 1.
So, to fix this first iterate through string and find that does any character is '_' if it is make flag=1.
I am providing you the correct code:-
#include<map>#include<set>#include<list>#include<cmath>#include<ctime>#include<deque>#include<queue>#include<stack>#include<string>#include<bitset>#include<cstdio>#include<limits>#include<vector>#include<climits>#include<cstring>#include<cstdlib>#include<fstream>#include<numeric>#include<sstream>#include<iostream>#include<algorithm>#include<unordered_map>usingnamespacestd;intmain(){intQ;cin>>Q;for(inta0=0;a0<Q;a0++){intn,count=0,flag=0,count1=0;cin>>n;stringb;cin>>b;// I added thisfor(inti=0;i<n;i++){if(b[i]=='_'){flag=1;break;}}//Now it works Correctlyfor(inti=0;i<n;i++){count=0;/*if(b[i]=='_')// Delete this commented part { flag=1; continue; }*/if(b[i]!='_'){for(intj=0;j<n;j++){if(b[i]==b[j])count++;}}if(count==1){break;}}if(count!=1&&flag==1){cout<<"YES"<<endl;continue;}elseif(flag==0){for(inti=0;i<n;i+=count1){count1=0;for(intj=i;b[j]!=b[i],j<n;j++){if(b[i]==b[j])count1++;}if(count1==1){break;}}}if(flag==0&&count1>1){cout<<"YES"<<endl;continue;}elsecout<<"NO"<<endl;}return0;}
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Happy Ladybugs
You are viewing a single comment's thread. Return to all comments →
The Input which does not works correctly in testcase 8 is:- 10 MY_MMM___ The Output must be NO but your program is giving YES. This error is happening because loop breaks before flag equals 1. So, to fix this first iterate through string and find that does any character is '_' if it is make flag=1.
I am providing you the correct code:-