Hackerrank - Strings - Two Strings
Given two strings, and , determine if they share a common substring.
Input Format
Constraints
- and consist of lowercase English letters only.
Output Format
For each 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:
- , . The substrings and are common to both and , so we print
YES
on a new line. - , . Because and have no common substrings, we print
NO
on a new line.
Solution :
# Enter your code here. Read input from STDIN. Print output to STDOUT
itt = int(raw_input())
for k in range(itt):
string_1 = list(raw_input())
string_2 = list(raw_input())
dic = { }
result = False
for l in string_1:
if l not in dic:
dic[l] = 1
else:
dic[l] += 1
for k in string_2:
if k in dic:
result = True
break
if result:
print "YES"
else:
print "NO"
Comments
Post a Comment