Two Strings | Lei Jiang Coding
Read full article from Two Strings | Lei Jiang Coding
You are given two strings, A and B. Find if there is a substring that appears in both A and B.
bool
hasSubStr(string A, string B){
if
( A.size() == 0 )
return
false
;
bool
map[26] = {
false
};
for
(
int
i = 0; i < A.size(); ++i)
map[ A[ i ] -
'a'
] =
true
;
for
(
int
j = 0; j < B.size(); ++j )
if
( map[ B[ j ] -
'a'
] )
return
true
;
return
false
;
}
int
main() {
/* Enter your code here. Read input from STDIN. Print output to STDOUT */
int
T;
cin >> T;
for
(
int
i = 0; i < T; ++i ){
string A, B;
cin >> A >> B;
bool
test = A.size() > B.size() ? hasSubStr(B, A) : hasSubStr(A, B);
if
( test )
cout <<
"YES"
<< endl;
else
cout <<
"NO"
<< endl;
}
return
0;
}