Chef and Feedback

Can anyone help me to know why I am getting TLE?

My code:
#include
#include
#include<string.h>
using namespace std;
int main() {
int t;
cin>>t;
while(t–)
{
int i,count=1;
string n;
cin>>n;
for(i=0;i<n.length()-2;i++)
{
if(n.substr(i,i+2)==“010”||n.substr(i,i+2)==“101”)
{
count=0;
break;
}
}
if(count==0)
cout<<“Good”<<endl;
else
cout<<“Bad”<<endl;
}
return 0;
}

Format your code using ``` before and after code.

Sorry did’t get that.

Enclose your code by putting ``` around it.

`
like this

I have put only one ` in the front otherwise you won’t be able to see it.
put three of these each on the first and last line

1 Like

most easiest way -

int main(){
fastio
lli t=1;
cin>>t;
while(t--) {
    string s;
    cin>>s;
    
    if(s.find("010") != string ::npos or s.find("101") != string ::npos)
        print("Good");
    else
        print("Bad");


  }
return 0;
}

Thank uu

Thank youu

cin>>t;
while(t--){
int i,count=1;
string n;
cin>>n;
for(i=0;i<n.length()-2;i++)
{
   if(n.substr(i,i+2)==“010”||n.substr(i,i+2)==“101”)
  {
   count=0; 
   break;
   }
}
if(count==0)
   cout<<“Good”<<endl;
else
   cout<<“Bad”<<endl;
}

two things wrong , updated code -

cin>>t;
while(t--){
int i,count=1;
string n;
cin>>n;
for(i=0;i<=n.length()-3;i++)
{
   if(n.substr(i,3)==“010”||n.substr(i,3)==“101”)
  {
   count=0; 
   break;
   }
}
if(count==0)
   cout<<“Good”<<endl;
else
   cout<<“Bad”<<endl;
}

Try this ,and give your submission link too.

int main(){
fastio
lli t=1;
cin>>t;
while(t--) {
    string s;
    cin>>s;
    
    lli count = 1;
    for(lli i=0;i<=sz(s)-3;i++)
    {
        if(s.substr(i,3) == "010" or s.substr(i,3)=="101")
            count=0;
    }
    if(count ==0)
        print("Good");
    else
        print("Bad");


  }
return 0;
}

Read substr function more clearly : “Substring in C++ - GeeksforGeeks

1 Like

THANK U MAN!!

1 Like