Printing Yes or No depending upon if two strings have a common substring

#include
#include
using namespace std;

int main
()
{

string s1,s2;

int p,c=0;

cin>>p;

for( ;p>0;p–)
{

getline(cin,s1);

getline(cin,s2);

for(int i=0;i<s1.length();i++)
{

for(int j=0;j<s2.length();j++)
{

if(s1[i]==s2[j])

{

c++;

}

}

}

if(c>0)

{

cout<<“yes”<<endl;

}

else
{
cout<<“no”<<endl;
}
}
return 0;
}

hello @ritik_99,
a) please save ur code here -> https://ide.codingblocks.com/
and share the generated link
b)
please explain me the problem statement

The problem is we need to print YES or NO depending upon if two string share a common substring .
suppose

  • mango
  • orange
    are two strings and they share o as common so it means YES we need to print

one simple approach is store frequency of each character of both strings in two different array and then check whether their exist any charater whose frequency is non zero in both the array.

in this way u can answer it in O(n) when n is lenght of largest string

Sir, i have already posted my approach. Kindly tell me whats wrong in that , its only printing NO for each test case. I will try your approach also but please tell me abt mine first ?

please save ur code on coding blocks ide. and share the link

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.