Given two strings, determine if they share a common substring. A substring may be as small as one character. For example, the words "a", "and", "art" share the common substring a
. The words "be" and "cat" do not share a substring.
Function Description
Complete the function twoStrings
in the editor below. It should return a string, either YES
or NO
based on whether the strings share a common substring.
twoStrings
has the following parameter(s):
s1
,s2
: two strings to analyze.
The first line contains a single integer p
, the number of test cases.
The following p
pairs of lines are as follows:
- The first line contains string
s1
. - The second line contains string
s2
s1
ands2
consist of characters in the range ascii[a-z].- 1 <=
p
<= 10 - 1 <=
|s1|
,|s2|
<= 105
For each pair of strings, return YES
or NO
.
Sample Input
2
hello
world
hi
world
Sample Output
YES
NO
Explanation
We have p = 2
pairs to check:
s1 = "hello"
,s2 = "world"
. The substrings"o"
and"l"
are common to both strings.a = "hi"
,b = "world"
.s1
ands2
share no common substrings.
import math
import os
import random
import re
import sys
# Complete the twoStrings function below.
def twoStrings(s1, s2):
if __name__ == '__main__':
fptr = open(os.environ['OUTPUT_PATH'], 'w')
q = int(input())
for q_itr in range(q):
s1 = input()
s2 = input()
result = twoStrings(s1, s2)
fptr.write(result + '\n')
fptr.close()
import math
import os
import random
import re
import sys
def twoStrings(s1, s2):
for w in s1:
if w in s2:
return "YES"
else:
return "NO"
if __name__ == '__main__':
fptr = open(os.environ['OUTPUT_PATH'], 'w')
q = int(input())
for q_itr in range(q):
s1 = input()
s2 = input()
result = twoStrings(s1, s2)
fptr.write(result + '\n')
fptr.close()
import math
import os
import random
import re
import sys
def twoStrings(s1, s2):
if len(set(s1).intersection(set(s2))) >= 1:
return "YES"
else:
return "NO"
if __name__ == '__main__':
fptr = open(os.environ['OUTPUT_PATH'], 'w')
q = int(input())
for q_itr in range(q):
s1 = input()
s2 = input()
result = twoStrings(s1, s2)
fptr.write(result + '\n')
fptr.close()