# Two Strings Hackerrank problem solution

##### Two Strings Hackerrank

Given two strings, a and b, determine if they share a common substring.

Input Format

The first line contains a single integer, p, denoting the number of (a,b) pairs you must check.
Each pair is defined over two lines:

The first line contains string a.
The second line contains string b.

Constraints

a and b consist of lowercase English letters only.
1<=p<=10 1<=|a|,|b|<=104

Output Format

For each (a,b) pair of strings, print YES on a new line if the two strings share a common substring; if no such common substring exists, print NO on a new line.

Sample Input

2
hello
world
hi
world

Sample Output

YES
NO

Explanation

We have pairs to check:

a=”hello”, b=”world”. The substrings o and l are common to both a and b, so we print YES on a new line.
a=”hi”, b=”world”. Because a and b have no common substrings, we print NO on a new line.

##### Solution

There are two concepts involved in solving this challenge:
Understanding that a single character is a valid substring.
Deducing that we only need to know that the two strings have a common substring — we don’t need to know what that substring is.
Thus, the key to solving this challenge is determining whether or not the two strings share a common character.
To do this, we create two sets, a and b, where each set contains the unique characters that appear in the string it’s named after. Because sets 26don’t store duplicate values, we know that the size of our sets will never exceed the letters of the English alphabet. In addition, the small size of these sets makes finding the intersection very quick.
If the intersection of the two sets is empty, we print NO on a new line; if the intersection of the two sets is not empty, then we know that strings and share one or more common characters and we print YES on a new line.

```#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>

using namespace std;

int main() {
int t;
cin>>t;
while(t--){
int flag=0;
string s,s1;
cin>>s>>s1;

for(int i=97;i<(97+25);i++)
{ char c=i;
if( s.find(c,0)!=std::string::npos && s1.find(c,0)!=std::string::npos)
{
flag = 1;
break;
}
}
if(flag)  cout<<"YES";
else cout<<"NO";
cout<<endl;
}

/* Enter your code here. Read input from STDIN. Print output to STDOUT */
return 0;
}
```
``````import java.util.*;

public class Solution {
static Set<Character> a;
static Set<Character> b;

public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int n = scan.nextInt();
for(int i = 0; i < n; i++) {
a = new HashSet<Character>();
b = new HashSet<Character>();
for(char c : scan.next().toCharArray()) {
}
for(char c : scan.next().toCharArray()) {
}

// store the set intersection in set 'a'
a.retainAll(b);

System.out.println( (a.isEmpty()) ? "NO" : "YES" );
}
scan.close();
}
}``````

### hasectic

A web developer(Front end and Back end), and DBA at csdamu.com. Currently working as Salesforce Developer @ Tech Matrix IT Consulting Private Limited. Check me @about.me/s.saifi