I am unable to find mistake help me!

Q:HackerRank in a String! | HackerRank
my code;
#include<bits/stdc++.h>
using namespace std;
int main()
{
int N,n;
cin>>N;
for(n=0;n<N;n++)
{
string s=“hackerrank”;
string s1;
int i,j,t=0,l=0,J=0;
cin>>s1;
l=s1.length();
for(i=0;i<10;i++)
{
for(j=0;j<l;j++)
{
if(s[i]=s1[j])
{
if(j>J)
{
J=j;
t=t+1;
break;
}
}
}
}
if(t==10)
{
cout<<“yes”;
}
else
{
cout<<“no”;
}
}
return 0;
}

You need to print “YES” or “NO”. Instead you are printing “yes”, “no”.

Make it capital letters first.’

Next time either provide an ideone link of your code or try putting your code inside ```and ``` so that its easier to read and can be copied to any editors

1 Like

why it is always printing “NO”?

#include<bits/stdc++.h>
using namespace std;
int
main ()
{
  int n, N = 0;
  for (n = 0; n < N; n++)
    {
      string s;
      string s1;
      s = "hackerrank";
      cin >> s1;
      int l, i, j, J, t = 0;
      l = s1.length ();
      for (i = 0; i < 9; i++)
	{
	  for (j + 0; j < l; j++)
	    {
	      if (s[i] == s1[j])
		{
		  if (j > J)
		    {
		      J = j;
		      t = t + 1;
		    }
		}
	    }
	}
      if (t == 10)
	{
	  cout << "YES";
	}
      else
	{
	  cout << "NO";
	}
    }
  return 0;
}

You haven’t initialized J(capital J). So the inner if condition never becomes true and hence the value of t remains zero always.

1 Like

You are setting N =0, due to which the loop does not run. Instead you should take it as input.

1 Like

AGAIN IT IS PRINTING ONLY “NO”;

#include<bits/stdc++.h>
using namespace std;
int
main ()
{
  int n, N = 0;
  cin>>N;
  for (n = 0; n < N; n++)
    {
      string s;
      string s1;
      s = "hackerrank";
      cin >> s1;
      int l, i, j, J=0 , t = 0;
      l = s1.length ();
      for (i = 0; i < 10; i++)
	{
	  for (j = 0; j < l; j++)
	    {
	      if (s[i]= s1[j])
		{
		  if (j > J)
		    {
		      J = j;
		      t = t + 1;
		      break;
		    }
		}
	    }
	}
      if (t == 10)
	{
	  cout << "YES";
	}
      else
	{
	  cout << "NO";
	}
    }
  return 0;
}

YEP BUT IT IS STILL PRINTING NO?

#include<bits/stdc++.h>
using namespace std;
int
main ()
{
 int n, N = 0;
 cin>>N;
 for (n = 0; n < N; n++)
   {
     string s;
     string s1;
     s = "hackerrank";
     cin >> s1;
     int l, i, j, J=0 , t = 0;
     l = s1.length ();
     for (i = 0; i < 10; i++)
   {
     for (j = 0; j < l; j++)
       {
         if (s[i]= s1[j])
   	{
   	  if (j > J)
   	    {
   	      J = j;
   	      t = t + 1;
   	      break;
   	    }
   	}
       }
   }
     if (t == 10)
   {
     cout << "YES";
   }
     else
   {
     cout << "NO";
   }
   }
 return 0;
}

In the innermost loop start the loop with " j=J+1" not from 0.
And also set J=-1 initially.

Hi.
Check line 22 and 24 in this code. There was your mistake. I have fixed it :-

#include<bits/stdc++.h>
using namespace std;
int
main ()
{
  int n, N = 0;
  cin>>N;
  for (n = 0; n < N; n++)
    {
      string s;
      string s1;
      s = "hackerrank";
      cin >> s1;
      int l, i, j, J=0 , t = 0;
      l = s1.length ();
      for (i = 0; i < 10; i++)
    {
      for (j = 0; j < l; j++)
        {
          if (s[i]== s1[j])
        {
          if (j >= J)
            {
              J = j + 1;
              t = t + 1;
              break;
            }
        }
        }
    }
      if (t == 10)
    {
      cout << "YES" << endl;
    }
      else
    {
      cout << "NO" << endl;
    }
    }
  return 0;
}

thanks,